1、实验代码
sem_demo.c
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>int main (void)
{
unsigned int sem_value = 4;sem_t *sem = sem_open ("posix_sem", O_RDWR | O_CREAT | O_EXCL, 0777, sem_value);if (sem == SEM_FAILED){
perror ("sem_open");exit (EXIT_FAILURE);}if (sem_getvalue (sem, &sem_value) != -1)printf ("the sem value: %d\n", sem_value);sem_wait (sem);sem_wait (sem);sem_wait (sem);sem_wait (sem);
// sem_wait (sem);sem_trywait (sem);if (sem_getvalue (sem, &sem_value) != -1)printf ("the sem value: %d\n", sem_value);sem_post (sem);sem_post (sem);sem_post (sem);sem_post (sem);sem_post (sem);sem_post (sem);if (sem_getvalue (sem, &sem_value) != -1)printf ("the sem value: %d\n", sem_value);if (sem_close (sem) != -1)printf ("sem close posix_sem success\n");printf ("wait for sem_unlink, 10s\n");sleep (10);if (sem_unlink ("posix_sem") != -1)printf ("sem_unlink posix_sem success\n");return 0;
}
实验结果:
sem_sync.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <fcntl.h>
#include <pthread.h>#define SEM_NAME "/posix_sem_operation"int main (void)
{
int i = 0;int j = 0;int ret_fork;int sem_val = 0;sem_t *sem;sem = sem_open (SEM_NAME, O_CREAT, 0666, 1);ret_fork = fork ();if (ret_fork == -1){
perror ("fork");sem_close (sem);sem_unlink (SEM_NAME);exit (EXIT_FAILURE);}if (ret_fork == 0){
while (i++ < 10){
// sem_trywait (sem);sem_wait (sem);sem_getvalue (sem, &sem_val);printf ("child process: sem value = %d\n", sem_val);sleep (1);}exit (EXIT_SUCCESS);}else if (ret_fork > 0){
while (j++ < 10){
sem_post (sem);sem_getvalue (sem, &sem_val);printf ("parent process: sem value = %d\n", sem_val);sleep (2);}}sem_close (sem);sem_unlink (SEM_NAME);return 0;
}
实验结果:
两个不相干进程同步
sem_wait.c
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>int main (void)
{
char *name = "/posix_sem";unsigned int sem_value = 4;sem_t *sem = sem_open (name, O_RDWR | O_CREAT, 0777, sem_value);if (sem == SEM_FAILED){
perror ("sem_open");exit (EXIT_FAILURE);}printf ("sem_open %s success\n", name);while (1){
if (sem_wait (sem) == -1){
perror ("sem_wait");exit (EXIT_FAILURE);}if (sem_getvalue (sem, &sem_value) != -1)printf ("wait process: sem value=%d\n", sem_value);sleep (1);}sleep (10);sem_close (sem);if (sem_unlink (name) != -1)printf ("sem_unlink %s success\n", name);return 0;
}
sem_post.c
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>int main (void)
{
char *name = "/posix_sem";unsigned int sem_value = 4;sem_t *sem = sem_open (name, O_RDWR | O_CREAT, 0777, sem_value);if (sem == SEM_FAILED){
perror ("sem_open");exit (EXIT_FAILURE);}printf ("sem_open %s success\n", name);while (1){
if (sem_post (sem) == -1){
perror ("sem_post");return -1;}if (sem_getvalue (sem, &sem_value) != -1)printf ("post process: sem value=%d\n", sem_value);sleep (5);}sleep (10);if (sem_unlink (name) != -1)printf ("sem_unlink %s success\n", name);return 0;
}
实验结果: