wait_pid代码分析

以下代码为网上的一段代码,注释是我自己加的,正确与否,若有异议,坑请赐教

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

void die(const char *msg)							//创建失败,则打印错误信息
{
        perror(msg);
        exit(1);											//exit(0)代表正常退出,exit(1)代表错误退出
}

void child2_do()
{
        printf("In child2: execute 'date'\n");

        sleep(5);																			//睡眠5秒,让其他进程执行
        if (execlp("date", "date", NULL) < 0)					//终端执行date,打印时间信息 
        {
                perror("child2 execlp");
        }
}

void child1_do(pid_t child2, char *argv)
{
        pid_t pw;

        do {
                if (*argv == '1') 
                {
                        pw = waitpid(child2, NULL, 0);
                }
                else 
                {
                        pw = waitpid(child2, NULL, WNOHANG);			//如果子进程2没有终止,则立即返回
                }
                if (pw == 0) 		//pw等于0时说明进程2没有退出,pw不等于0时,说明进程2已经退出
                {
                        printf("In child1 process:\nThe child2 process has not exited!\n");
                        sleep(1);
                }
        }while (pw == 0);

        if (pw == child2) 				//进程2正常退出时,则在终端显示进程2的pid,并执行pwd
        {
                printf("Get child2 %d.\n", pw);
                sleep(5);
                if (execlp("pwd", "pwd", NULL) < 0) {
                        perror("child1 execlp");
                }
        }
        else {
                printf("error occured!\n");
        }
}

void father_do(pid_t child1, char *argv)
{
        pid_t pw;

        do {
                if (*argv == '1') {
                        pw = waitpid(child1, NULL, 0);
                }
                else {
                        pw = waitpid(child1, NULL, WNOHANG);
                }

                if (pw == 0) {
                        printf("In father process:\nThe child1 process has not exited.\n");
                        sleep(1);
                }
        }while (pw == 0);

        if (pw == child1) {
                printf("Get child1 %d.\n", pw);
                if (execlp("ls", "ls", "-l", NULL) < 0) {
                        perror("father execlp");
                }
        }
        else {
                printf("error occured!\n");
        }
}

int main(int argc, char *argv[])
{
        pid_t child1, child2;

        if (argc < 3)						//如果输入的参数少于3个,则打印如下信息 
        {
                printf("Usage: waitpid [0 1] [0 1]\n");
                exit(1);
        }

        child1 = fork();																//创建子进程1

        if (child1 < 0) {
                die("child1 fork");
        }
        else if (child1 == 0) 
        	{													
                child2 = fork();						//在子进程child1中再次创建一个子进程child2

                if (child2 < 0) {
                        die("child2 fork");
                }
                else if (child2 == 0) 
                {
                        child2_do();				//检验如果创建的child2的pid==0,则执行child2_do();
                }
                else 												//检验如果创建的child2的pid!=0,则执行child1_do();
                {
                        child1_do(child2, argv[1]);			//注意:输入参数为进程2的pid,argv[1]
                }
        }
        else									//如果是父进程,则执行father_do  
        {
                father_do(child1, argv[2]);
        }

        return 0;
}
终端上执行的结果明天在给出,现在太晚了,要睡了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个使用C语言实现的简单的ICMP协议分析代码,它可以发送和接收ICMP协议报文: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <errno.h> #include <time.h> #include <netinet/ip_icmp.h> #define PACKET_SIZE 4096 #define MAX_WAIT_TIME 5 #define MAX_NO_PACKETS 3 char sendpacket[PACKET_SIZE]; char recvpacket[PACKET_SIZE]; int sockfd, datalen = 56; int nsend = 0, nreceived = 0; struct sockaddr_in dest_addr; pid_t pid; // 计算校验和 unsigned short cal_chksum(unsigned short *addr, int len) { int nleft = len; int sum = 0; unsigned short *w = addr; unsigned short answer = 0; while (nleft > 1) { sum += *w++; nleft -= 2; } if (nleft == 1) { *(unsigned char *)(&answer) = *(unsigned char *)w; sum += answer; } sum = (sum >> 16) + (sum & 0xffff); sum += (sum >> 16); answer = ~sum; return answer; } // 发送ICMP报文 void send_packet() { memset(sendpacket, 0, sizeof(sendpacket)); struct icmp *icmp_hdr = (struct icmp *)sendpacket; icmp_hdr->icmp_type = ICMP_ECHO; icmp_hdr->icmp_code = 0; icmp_hdr->icmp_cksum = 0; icmp_hdr->icmp_id = pid; icmp_hdr->icmp_seq = nsend++; memset(icmp_hdr->icmp_data, 0xa5, datalen); gettimeofday((struct timeval *)icmp_hdr->icmp_data, NULL); icmp_hdr->icmp_cksum = cal_chksum((unsigned short *)icmp_hdr, datalen + 8); sendto(sockfd, sendpacket, datalen + 8, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr)); } // 接收ICMP报文 void recv_packet() { int n; socklen_t fromlen; extern int errno; fromlen = sizeof(dest_addr); while (1) { if ((n = recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&dest_addr, &fromlen)) < 0) { if (errno == EINTR) { continue; } perror("recvfrom error"); return; } struct iphdr *ip_hdr = (struct iphdr *)recvpacket; int ip_hdr_len = ip_hdr->ihl << 2; struct icmp *icmp_hdr = (struct icmp *)(recvpacket + ip_hdr_len); int icmp_len = n - ip_hdr_len; if (icmp_hdr->icmp_type == ICMP_ECHOREPLY && icmp_hdr->icmp_id == pid) { struct timeval *tvsend = (struct timeval *)icmp_hdr->icmp_data; struct timeval tvrecv; gettimeofday(&tvrecv, NULL); double rtt = (tvrecv.tv_sec - tvsend->tv_sec) * 1000.0 + (tvrecv.tv_usec - tvsend->tv_usec) / 1000.0; printf("%d bytes from %s: icmp_seq=%u ttl=%d time=%.1f ms\n", icmp_len, inet_ntoa(dest_addr.sin_addr), icmp_hdr->icmp_seq, ip_hdr->ttl, rtt); nreceived++; } } } // 主函数 int main(int argc, char *argv[]) { struct hostent *host; struct protoent *protocol; unsigned long inaddr = 0l; int waittime = MAX_WAIT_TIME; int size = 50 * 1024; if (argc < 2) { printf("usage:%s hostname/IP address\n", argv[0]); exit(1); } if ((protocol = getprotobyname("icmp")) == NULL) { perror("getprotobyname"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_RAW, protocol->p_proto)) < 0) { perror("socket error"); exit(1); } setuid(getuid()); setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)); bzero(&dest_addr, sizeof(dest_addr)); dest_addr.sin_family = AF_INET; if (inaddr = inet_addr(argv[1]) == INADDR_NONE) { if ((host = gethostbyname(argv[1])) == NULL) { perror("gethostbyname error"); exit(1); } memcpy((char *)&dest_addr.sin_addr, host->h_addr, host->h_length); } else { dest_addr.sin_addr.s_addr = inaddr; } pid = getpid(); printf("PING %s(%s): %d bytes data in ICMP packets.\n", argv[1], inet_ntoa(dest_addr.sin_addr), datalen); while (nsend < MAX_NO_PACKETS) { send_packet(); recv_packet(); sleep(waittime); } printf("--- %s ping statistics ---\n", argv[1]); printf("%d packets transmitted, %d received, %%%d lost\n", nsend, nreceived, (nsend - nreceived) / nsend * 100); close(sockfd); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月光下的麦克

您的犒赏是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值