xoxo.c

来自「three proceses , one proces read some me」· C语言 代码 · 共 97 行

C
97
字号
#include <stdio.h>       #include <sys/types.h>       #include <unistd.h>#include <sys/ipc.h>#include <sys/shm.h>#include <string.h>#define SHMSZ     27int main(){	int shmid;	int *shm;	key_t key;	key =ftok("./",1);	if((shmid=shmget(key,SHMSZ,IPC_CREAT | 0666))<0){		perror("shmget");		return 1;	}	if ((shm=shmat(shmid,NULL,0))==(int *) -1){		perror("shmat");		return 1;	}		*shm=0;switch(fork()){	case -1: {		perror("eroare fork\n");		break;		}	case 0:{ 		printf("%d - fiu\n",getpid());		switch(fork()){		case -1: {			perror("eroare fork\n");			break;			}		case 0:{ 			printf("%d - nepot\n",getpid());			char *str;			strcpy(str,"GOL");			while (strcmp(str,"EXIT")!=0)			{				printf("mesaj \n");				scanf("%s",str);			}			int nepot_id=getpid();			*shm=nepot_id;						break;			}		default:{			//fiuuuuuuuuuuuuuuuuuuuuuuuuuuuu			char *u;			while (*shm == 0)				sleep(1);			kill(*shm,9);			printf("nepot killarit la pid %d \n",*shm);			*shm=getpid();			kill(getpid(),9);		//	printf("tata homorat \n");			break;			}		}		break;		}	default:{		printf("%d - tata\n",getpid());		sleep(2);		/*if (*shm!=0)		printf("tata killarit la pid %d \n",*shm);	*/	break;		}}sleep(10);return 0; }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?