Linux C程序练习(4)进程通信之信号量、共享内存

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <sys/types.h>

#define MAX_TEXT 1024


int main(int argc,char **argv){
	int semid=0;
	int count=0;
	pid_t pd=0;
	struct sembuf sops;
	
	semid=semget((key_t)12345,3,0666|IPC_CREAT);
	if(semid==-1){
		perror("semget()");
		exit(1);
	}
	printf("Begin fork()\n");
	
	for(count=0;count<3;count++){
		pd=fork();
		if(pd<0){
			perror("fork()");
			exit(1);
		}
		if(pd==0){
			printf("child[%d] creat!\n",getpid());
			
			sops.sem_num=count;
			sops.sem_op=-1;
			sops.sem_flg=0;
			
			if(semop(semid,&sops,1)==-1){
				perror("semop()");
				exit(1);
			}
			
			printf("child [%d] exit!\n",getpid());
			exit(0);
		}
	}
	exit(0);
}

semread1.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/sem.h>
#include <sys/ipc.h>
#include <sys/types.h>

#define MAX_TEXT 1024


int main(int argc,char **argv){
	int semid=0;
	struct sembuf sops;
	
	if(argc!=2){
		printf("sem_send:usage error.\n");
		exit(1);
	}
	semid=semget((key_t)12345,3,0666|IPC_CREAT);
	if(semid==-1){
		perror("semget()");
		exit(1);
	}
	
	if(strncmp(argv[1],"0",1)==0){
		sops.sem_num=0;
	}else if(strncmp(argv[1],"1",1)==0){
		sops.sem_num=1;
	}else if(strncmp(argv[1],"2",1)==0){
		sops.sem_num=2;
	}else{
		printf("argument:count error.\n");
		exit(1);
	}
	
	sops.sem_op=1;
	sops.sem_flg=SEM_UNDO;
	
	if(semop(semid,&sops,1)==-1){
		perror("semop()");
		exit(1);
	}else{
		printf("semop(%d) over.\n",sops.sem_num);
	}
	exit(0);
}

semwrite1.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/types.h>

int main(int argc,char **argv){
	int fd=0;
	int shmid=0;
	char *buf;
	
	fd=open("/etc/passwd",O_RDONLY);
	if(fd<0){
		perror("open()");
		exit(1);
	}
	shmid=shmget((key_t)12345,4096,0666|IPC_CREAT);
	if(shmid<0){
		perror("shmget()");
		exit(1);
	}
	
	buf=(char *)shmat(shmid,NULL,0);
	if(buf==(void *)-1){
		perror("shmat()");
		exit(1);
	}
	
	if(read(fd,buf,1024)==-1){
		perror("read()");
		exit(1);
	}else{
		printf("write successful.\n");
	}
	
	if(shmdt(buf)==-1){
		perror("shmdt()");
		exit(1);
	}
	exit(0);
}

sharedMemoryread1.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/types.h>

int main(int argc,char **argv){
	int fd=0;
	int shmid=0;
	char *buf;
	
	shmid=shmget((key_t)12345,4096,0666|IPC_CREAT);
	if(shmid<0){
		perror("shmget()");
		exit(1);
	}
	
	buf=(char *)shmat(shmid,NULL,0);
	if(buf==(void *)-1){
		perror("shmat()");
		exit(1);
	}
	
	if(strcmp(buf,"")==0){
		printf("readnothing\n");
	}else{
		printf("read: %s\n",buf);
	}
	
	if(shmdt(buf)==-1){
		perror("shmdt()");
		exit(1);
	}
	exit(0);
}

sharedMemorywrite1.c

这两种通信方式比较前一博客(3)中,使用的较多,共享内存也可以参考一下ftok函数。

猜你喜欢

转载自blog.csdn.net/u014717398/article/details/79463517
今日推荐