Home » Linux » POSIX famous Linux signal under the simple example

POSIX famous Linux signal under the simple example

#include 
#include 
#include 
#include 
#include 
#include 
#include 
int (main)
{
 sem_t *s = sem_open ("log", O_CREAT | O_EXCL, 0666, 0);
 if (s = SEM_FAILED)
{

 PERROR ("sem_open");
 exit (EXIT_FAILURE);
}
 pid_t pid;
 = fork (PID);
 if (PID > 0)
{

 puts ("parent process sleeping");
 sleep (5);
 sem_post (s);
}
 else if (PID = 0)
{

 puts ("child process waiting");
 sem_wait (s);
 puts ("child get SEM !");
}
 sem_unlink ("log");
 return 0;
}


PS: (1) POSIX signal compiler needs additional link library. The use of -lrt or lpthread. in Ubuntu 12.04 is -lpthread, may be -lrt.

in other systems

(2) before use in the current directory to a log file, you can use the touch command to create (touch log

).

(3) POSIX can view the signal in the current system of.

in /dev/shm

(4) POSIX famous signal is mainly used for process synchronization. POSIX memory signal (ring signal) between can be used to process, can also be used for synchronization between.

multi-threaded process under the single

Latest