5.21 POSIX 信号量

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

实验结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值