I'm learning threads and my code runs upto last print statement. Why it is giving segmentation fault at print? I think possible reason could be non-existant address passed as argument to print, but it is not the reason, I'm passing valid address.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *thread (void *vargp) {
int arg = *((int*)vargp);
return &arg;
}
int main () {
pthread_t tid;
int thread_arg = 0x7ffdbc32fa34;
int *ret_value;
pthread_create(&tid, NULL, thread, &thread_arg);
pthread_join(tid, (void **)(&ret_value));
printf("hello\n");
printf("%X\n", *ret_value);
return 0;
}
It is giving following output:
hello
Segmentation fault (core dumped)
Is it because I'm returning an address of a local variable, which gets destroyed once thread is returned? I don't think so, because changing to following code is also giving me segmentation fault!
void *thread (void *vargp) {
int * arg = malloc(sizeof(int));
*arg = *((int*)vargp);
return &arg;
}
Is it because I'm returning an address of a local variable, which gets
destroyed once thread is returned?
Yes, it is.
I don't think so, because changing to following code is also giving me
segmentation fault!
This code is also returning the address of a local variable (return &arg;). Instead, you should be returning the pointer value that malloc() returned (return arg;):
void *thread (void *vargp)
{
int * arg = malloc(sizeof(int));
*arg = *((int*)vargp);
return arg;
}
You also should not be casting the address of ret_value to type void ** in main() - the variable is of type int * not void *, so it shouldn't be written to through a void ** pointer (although, in practice, this will usually work). Instead, you should be using void * variable to hold the return value, then either casting this value to int * or assigning it to a variable of type int *:
void *ret_value;
pthread_create(&tid, NULL, thread, &thread_arg);
pthread_join(tid, &ret_value);
printf("%X\n", *(int *)ret_value);
Related
I am unable to detect the memory error(memcheck error) . when i run the code i see some unexpected output came. so please describe what is happening in thes code.
#include <stdio.h>
#include <stdlib.h>
char *getString()
{
char message[100]="Hello World";
char *ret = message;
return ret;
}
void test4()
{
printf("String: %s",getString());
}
int main()
{
test4();
return 0;
}
Your variable message is local to the function getString and is on the stack. After getString returns, message no longer exists, meaning that the pointer returned by getString, which was set to point to message is no longer valid.
I was trying to communicate between two processes using Shared Memory concept. But here, though I have pointed the shared memory addresses of different variables to different files, they seem to be connected. As soon as I alter value of one variable, the new value overwrites on other variable too, in this case, se1->val and se2->val are coming out to be connected. Can someone help why it's happening so?
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#define s(t) scanf("%d",&t)
#define p(t) printf("%d ",t)
struct sem
{
int val;
int xy;
};
struct sem* se1;
struct sem* se2;
int main()
{
printf("You in P1\n");
key_t key1,key2;
key1=ftok("shmfile1",0);
key2=ftok("shmfile3",0);
int shmid1=shmget(key1, sizeof(struct sem),0644|IPC_CREAT);
int shmid2=shmget(key2, sizeof(struct sem),0644|IPC_CREAT);
se1=shmat(shmid1,NULL,0);
se2=shmat(shmid2,NULL,0);
se1->xy=4;
se2->xy=8;
se1->val=0;
se2->val=1;
int r=10;
while(r--)
{
printf("\nIn P1 process ");
while(se2->val==0);
se2->val--;
se1->xy=se2->xy+1;
se1->val++;
p(se1->xy);
p(se2->xy);
}
return 0;
}
It is expected se1->val and se2->val will lead to semaphore type results, but due to overwriting it's not happening!
I'm trying to write a small database program which will have 5 functions, the first one is Add() but I get SegFault error on scanf:
void Add();
struct data{
char name[20];
char description[300];
int quantity;
};
typedef struct data dataobj;
dataobj element;
int main()
{
Add();
return 0;
}
Add() {
FILE *database;
database = fopen("database.txt", "a+");
printf("Object: \n");
fgets(element.name,20,stdin);
fprintf(database, element.name);
printf("Description: \n");
fgets(element.description,300,stdin);
fprintf(database, element.description);
printf("Quantity: \n");
scanf("%d",&element.quantity);
fprintf(database, element.quantity);
fclose(database);
}
this is the error: Program received signal SIGSEGV, Segmentation fault.
In ungetwc () (C:\WINDOWS\SysWOW64\msvcrt.dll)
debugger window:
#0 0x77bea965 ungetwc() (C:\WINDOWS\SysWOW64\msvcrt.dll:??)
#1 0x77c21268 msvcrt!_iob() (C:\WINDOWS\SysWOW64\msvcrt.dll:??)
#2 ?? ?? () (??:??)
Also I noticed that if I write fgets after scanf instruction, fgets will not be executed for some reasons.. So, in the prototype I had to keep this order: char char int (for example I couldnt write: char int char)
Solved, I was trying to print the int directly, I should have used:
printf("Quantity: \n");
scanf("%d", &element.quantity);
fprintf(database,"%d", element.quantity);
forgot the %d
I have started working on POSIX threads. I wrote a simple code.
My question is on Mutex.
Initializing the mutex inside threaded function gives wrong result. While initializing the mutex inside main function (before creation of threads) gives proper result. Why is that happening?
The count value is expected to be 200000 but it is showing some improper value < 200000.
Here is my code.
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <malloc.h>
void *thread_handler (void *name);
unsigned long int count=0;
pthread_mutex_t lock;
void main () {
pthread_t thread_num[2];
pthread_attr_t attr;
pthread_attr_init (&attr);
int i;
for (i=0;i<2;i++) {
if (pthread_create (&thread_num[i],&attr,(void *) thread_handler,NULL)<0) {
printf ("\n Error in Creating the Threads");
}
}
for (i=0;i<2;i++) {
pthread_join(thread_num[i],NULL); //Waiting for the Thread to Exit
}
printf ("\n The value of count=%ld\n",count);
}
void *thread_handler (void *arg) {
int i;
if (pthread_mutex_init (&lock,NULL)!=0) {
printf ("\n Error in Initializing the Mutex");
}
pthread_mutex_lock (&lock);
for (i=0;i<100000;i++) {
count++;
}
pthread_mutex_unlock(&lock);
pthread_exit(NULL);
}
Thanks in Advance,
NDPrasad.
When you initialize the mutex inside the thread_handler function, it is initialized twice(because you create two threads that execute this function). It causes undefined behavior(which means that anything can happen).
A quote from here:
Attempting to initialize an already initialized mutex results in undefined behavior.
I am getting problem when I am giving pthread_mutex_attr as a parameter while creating thread. If I pass attribute as NULL it is working fine, but not desired this NULL,
Here I am posting code, Please help me correct my mistakes and learn things.
pthread_mutex_t thread_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_t thread1;
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&thread_mutex, &attr);
ret = pthread_create(&thread1, NULL, my_func, (void *)message);
pthread_mutexattr_destroy(&attr);
pthread_mutex_destroy(&thread_mutex);
pthread_exit(NULL);
if I pass attr like this
ret = pthread_create(&thread1,&attr,upload_data,(void *)message);
This is giving segmentation fault.
I am not sure what kind of error you get as you never specified what exactly your problem is but I assume you can't compile your code.
From man pthread_create:
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
This shows that pthread_create doesn't use pthread_mutex_attr but pthread_attr