linuxC多进程通讯----POSIX信号量

≡放荡痞女 提交于 2020-02-08 09:39:07

相关API

•sem_t *sem_open (const char *name, int oflag);
•sem_t *sem_open (const char *name, int oflag,mode_t mode, unsigned int value);
•int sem_close (sem_t *sem);
•int sem_post (sem_t *sem);
•int sem_wait (sem_t *sem);
•int sem_trywait (sem_t *sem);
•int sem_timedwait (sem_t *sem, const struct timespec *abs_timeout);
•int sem_unlink (const char *name);
•int sem_getvalue (sem_t *sem, int *sval);

使用说明

•包含头文件:#include <semaphore.h>
•编译时要指定:-lpthread
•Pthread:
–POSIX threads,操作线程的API标准
–适用于 Unix、Linux、Mac OS

基本创建、加减、关闭示例

#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;
}

两个进程通过信号量通讯示例

post

#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;
}

wait

#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;
}

父子进程通过信号量同步

#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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!