21 semaphore 3

1. 示例,多进程间信号灯通信:

需要先执行Client程序,再执行Server。

Server端程序

#include "stdio.h"
#include "stdlib.h"
#include "pthread.h"
//#include "semaphore.h"
#include "sys/ipc.h"
#include "sys/sem.h"
#include "unistd.h"
//sem_t sem;
union semun 
{	                
	int  val;  	/* Value for SETVAL*/
	struct semid_ds *buf;    /* Buffer for IPC_STAT, IPC_SET */
								                  
	unsigned short  *array;  /* Array for GETALL, SETALL */
												                 
	struct seminfo  *__buf;  /* Buffer for IPC_INFO(Linux-specific) */
};

int semid;
union semun mysemun;
struct sembuf mysembuf;
int main()//main thread code
{
  int i;
  int key;
  key=ftok("./a.c",'a');
  if(key < 0)
  {
	printf("creat key failure\n");
	return -1;
  }
  printf("creat key sucess \n");
  semid=semget(key,3,IPC_CREAT | 0777);//IPC_EXCL
  if(semid < 0)
  {
	printf("creat semaphore failure\n");
	return -2;
  }
  printf("creat semaphore sucess,semid=%d\n",semid);
  system("ipcs -s");
  //init sem
  //mysemun.val=0;
  //semctl(semid,0,SETVAL,mysemun);

  mysembuf.sem_num=0;
  mysembuf.sem_flg=0;
  for(i=0;i<10;i++)//first 
  {
	usleep(100);
	printf("this is main fun i=%d\n",i);
  }
  //V
  mysembuf.sem_op=1;
  semop(semid,&mysembuf,1);
  while(1);
  return 0;
}

执行结果:

alex@alex-VirtualBox:~/Share/process/twenty-one$ ./server
creat key sucess
creat semaphore sucess,semid=65537

------ Semaphore Arrays --------
key        semid      owner      perms      nsems
0x00000000 32768      alex       777        3
0x6101657b 65537      alex       777        3

this is main fun i=0
this is main fun i=1
this is main fun i=2
this is main fun i=3
this is main fun i=4
this is main fun i=5
this is main fun i=6
this is main fun i=7
this is main fun i=8
this is main fun i=9



Client端程序

#include "stdio.h"
#include "stdlib.h"
#include "pthread.h"
//#include "semaphore.h"
#include "sys/ipc.h"
#include "sys/sem.h"
#include "unistd.h"
//sem_t sem;
union semun 
{	                
	int  val;  	/* Value for SETVAL*/
	struct semid_ds *buf;    /* Buffer for IPC_STAT, IPC_SET */
								                  
	unsigned short  *array;  /* Array for GETALL, SETALL */
												                 
	struct seminfo  *__buf;  /* Buffer for IPC_INFO(Linux-specific) */
};

int semid;
union semun mysemun;
struct sembuf mysembuf;
int main()//main thread code
{
  int i;
  int key;
  key=ftok("./a.c",'a');
  if(key < 0)
  {
	printf("creat key failure\n");
	return -1;
  }
  printf("creat key sucess \n");
  semid=semget(key,3,IPC_CREAT | 0777);//IPC_EXCL
  if(semid < 0)
  {
	printf("creat semaphore failure\n");
	return -2;
  }
  printf("creat semaphore sucess,semid=%d\n",semid);
  system("ipcs -s");
  //init sem
  mysemun.val=0;
  semctl(semid,0,SETVAL,mysemun);

  mysembuf.sem_num=0;
  mysembuf.sem_flg=0;
  //P   wait
  mysembuf.sem_op=-1;
  semop(semid,&mysembuf,1);
  for(i=0;i<10;i++)//second 
  {
	usleep(100);
	printf("this is main fun i=%d\n",i);
  }
  //V
  //mysembuf.sem_op=1;
  //semop(semid,&mysembuf,1);
  while(1);
  return 0;
}

执行结果:

alex@alex-VirtualBox:~/Share/process/twenty-one$ ./client
creat key sucess
creat semaphore sucess,semid=65537

------ Semaphore Arrays --------
key        semid      owner      perms      nsems
0x00000000 32768      alex       777        3
0x6101657b 65537      alex       777        3

this is main fun i=0
this is main fun i=1
this is main fun i=2
this is main fun i=3
this is main fun i=4
this is main fun i=5
this is main fun i=6
this is main fun i=7
this is main fun i=8
this is main fun i=9


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值