8.13 (Day 7)IO进程线程

1:父子进程同时登记 SIGINT 信号

父进程的处理方式:输出我是父进程

子进程的处理方式:输出我是子进程

代码如下:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;

pid_t pid;

void hander(int signum)
{
	if(SIGINT == signum)
	{
		printf("\b\b  \b\b");
		printf("我是父进程\n");
		sleep(1);
		exit(0);
	}
	
}
void hander1(int signum)
{
	if(SIGINT == signum)
	{
		printf("\b\b  \b\b");
		printf("我是子进程\n");
		sleep(1);
		exit(0);

	}
}
int main(int argc, const char *argv[])
{
	pid_t pid = fork();
	if(pid > 0)
	{
		signal(SIGINT,hander);
	}
	else if(pid == 0)
	{
		signal(SIGINT,hander1);
	}
	while(1);
	return 0;
}

运行结果:

2:当一个子进程死亡的时候,会自动的向其父进程发出SIGCHLD信号

要求实现:当父进程接收到子进程死亡信息的时候,回收子进程的资源,防止僵尸进程的出现

代码如下:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;

pid_t pid;
void hander(int signum)
{
	if(SIGCHLD == signum)
	{
		wait(0);
		printf("回收成功\n");
	}
}
int main(int argc, const char *argv[])
{
	pid_t pid = fork();
	if(pid > 0)
	{
		signal(SIGCHLD,hander);
		while(1);
	}
	else if(pid == 0)
	{
		exit(0);
	}
	return 0;
}

运行结果:

3:当一个子进程死亡的时候,会自动的向其父进程发出SIGCHLD信号

要求实现:当父进程接收到子进程死亡信息的时候,回收子进程的资源,防止僵尸进程的出现

追加要求:父进程使用循环创建500个子进程,每一个子进程创建完毕后,立刻死亡

要求测试,是否产生僵尸进程,并做到没有僵尸进程

代码如下:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;

int i=0;
void hander(int signum)
{
	if(SIGCHLD == signum)
	{
		i++;
		printf("%d\n",i);
	while(1)
	{
		pid_t pid = waitpid(-1,0,WNOHANG);
		if(pid == -1){ return; }
	}
	}
}
int main(int argc, const char *argv[])
{
	signal(SIGCHLD,hander);
	for(int i=0; i<500; i++)
	{
		pid_t pid = fork();
		if(pid == 0)
		{
			exit(0);
		}
	}
	while(1)
	{
		printf("1\n");
		sleep(1);
	}
	return 0;
}

运行结果:

4:有2个.c文件,每个.c文件都拥有一对父子进程,总共4个进程 A a B b 现在要求实现一个多米诺骨牌的效果: 按ctrl+c结束a进程的运行,a进程结束运行之前,通过kill函数向b进程发送SIGINT信号,b进程死亡后,B进程回收b进程的资源后,大B进程再使用kill函数向A进程发送SIGTSTP信号后,大B进程结束运行。A进程接受到B进程的SIGTSTP信号后,会后a进程的资源后也结束运行 注意:kill函数要求获得另一个进程的pid,使用文件IO 这个题需要语序一个函数叫做 kill 函数

(1)A.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;

pid_t pid,b_pid,A_pid;
void handler1(int signum)
{
	if(signum==SIGINT)
	{
		if(pid==0)
		{
			int fd=open("./1.txt",O_RDONLY);
			read(fd,&b_pid,sizeof(pid_t));
			close(fd);
			printf("b_pid=%d\n",b_pid);
			kill(b_pid,SIGINT);
			printf("a# dead\n");
			exit(0);
		}
	}
}

void handler2(int signum)
{
	if(signum==SIGTSTP)
	{
		if(pid>0)
		{
			wait(0);
			printf("A# dead\n");
			exit(0);

		}
	}
}
int main(int argc, const char *argv[])
{
	signal(SIGINT,handler1);
	signal(SIGTSTP,handler2);


	pid=fork();
	if(pid>0)
	{
		A_pid=getpid();
		printf("A_pid=%d\n",A_pid);
int fd1=open("./2.txt",O_WRONLY|O_TRUNC|O_CREAT,0664);
	write(fd1,&A_pid,sizeof(pid_t));
	close(fd1);
	while(1)
	{
		sleep(1);
	}

	}
	else if(pid == 0)
	{
		while(1)
		{
			sleep(1);
		}
	}
	return 0;
}

(2)B.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;

pid_t pid,bpid,A_pid;
void handler2(int signum)
{
	if(signum==SIGINT)
	{
		if(pid==0)
		{
			printf("b# dead\n");
			exit(0);
		}
	}
	if(signum==SIGCHLD)
	{
		if(pid>0)
		{
			
			int fd1=open("./2.txt",O_RDONLY);
			read(fd1,&A_pid,sizeof(pid_t));
			printf("A_pid=%d\n",A_pid);
			close(fd1);


			kill(A_pid,SIGTSTP);
			wait(0);
			printf("B# dead\n");
			exit(0);
		}
	}
}
int main(int argc, const char *argv[])
{
	signal(SIGINT,handler2);
	signal(SIGCHLD,handler2);
	pid=fork();
	if(pid==0)
	{
		b_pid=getpid();
		printf("bpid=%d\n",b_pid);
int fd=open("./1.txt",O_WRONLY|O_TRUNC|O_CREAT,0664);
	write(fd,&b_pid,sizeof(pid_t));
	close(fd);

	}
		while(1);
	return 0;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值