1822909 Members
3748 Online
109645 Solutions
New Discussion юеВ

sem_wait, core dump

 
Praphul Menon_1
Occasional Contributor

sem_wait, core dump

The following code snippet throws a Memory fault(coredump)on HP-UX 11i
---------------8<----------------------
#include

int main (int argc, char *argv[]){
sem_t* semt;
semt = sem_init(semt,1,0);

if (sem_wait(semt) < 0 )
perror ("wait error ");
}
---------------8<----------------------
The program was compiled with the following command line
cc -o sem_try -g try_sem.c -lrt

What seems to be wrong with the program ...

regards
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor

Re: sem_wait, core dump

Well the fundamental problem is your sem_init. Note that no memory has been allocated for what semt points to and also sem_init returns an int.

sem_t semt;
int cc = 0;

cc = sem_init(&semt,1,0);
if (cc == 0)
{
if (sem_wait(&semt) < 0) perror("wait_error");
}


If it ain't broke, I can fix that.
Praphul Menon_1
Occasional Contributor

Re: sem_wait, core dump

Thanks for the response, how can the same functionality be acheived using sem_open instead of sem_init ...
A. Clay Stephenson
Acclaimed Contributor

Re: sem_wait, core dump

Well, here's a 3 minute example. Note that you can safely use O_CREAT each time to create a named semaphore and it will create one if needed. The sem_unlink should probably only be used when the sema4 is no longer needed.

Attached is sem.c

Compile like this:

cc sem.c -lrt -o sem
If it ain't broke, I can fix that.