使用execl函数写出进程上下半复制图片,写出僵尸进程,孤儿进程和守护进程

1.execl函数

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(char argc, char *argv[])
{
    int op;
    op = open("1.jpeg", O_CREAT | O_TRUNC, 0777);
    if (op < 0)
    {
        perror("open");
        return -1;
    }
    close(op);
    pid_t res = fork();
    int fd = open("/home/gec/Downloads/629e6b7c99237e335dd98f06a6cd51ab.jpeg", O_RDONLY);
    if (fd < 0)
    {
        perror("open");
        return -1;
    }
    size_t size = lseek(fd, 0, SEEK_END);
    int flag;
    if (size % 2 == 0) // 解决文件大小奇偶性对半问题
        flag = 0;
    else
        flag = 1;
    lseek(fd, 0, SEEK_SET);
    char buf[255] = {0};
    int up;
    up = open("1.jpeg", O_WRONLY);
    if (up < 0)
    {
        perror("open");
        return -1;
    }
    if (res > 0)
    {
        //sleep(3);
        char arr1[20]={0};
        sprintf(arr1,"%d",fd);
        char arr2[20]={0};
        sprintf(arr2,"%d",up);
        char arr3[20]={0};
        sprintf(arr3,"%ld",size);
        char arr4[20]={0};
        printf("afafaf\n");
        sprintf(arr4,"%d",flag);
        execl("up","up",arr1,arr2,arr3,arr4,NULL);
        /*for (size_t i = 0; i < size / 2 + flag; i++) // 父进程复制上半截,某些地方没必要判断
        {
            read(fd, buf, 1);
            write(up, buf, 1);
            memset(buf, 0, sizeof(buf));
        }*/
    }
    if (res == 0) // 子进程复制后半段
    {
        char arr1[20]={0};
        sprintf(arr1,"%d",fd);
        char arr2[20]={0};
        sprintf(arr2,"%d",up);
        char arr3[20]={0};
        sprintf(arr3,"%ld",size);
        char arr4[20]={0};
        printf("afafaf\n");
        sprintf(arr4,"%d",flag);
        execl("down","up",arr1,arr2,arr3,arr4,NULL);
        //sleep(1);
        /*lseek(fd, size / 2, SEEK_SET);
        lseek(up, size / 2, SEEK_SET);
        for (size_t i = 0; i < size / 2 + flag; i++)
        {
            memset(buf, 0, sizeof(buf));
            if (read(fd, buf, 1) < 0)
            {
                perror("read");
                break;
            }
            write(up, buf, 1);
        }*/
        printf("afaf\n");
        close(fd);
        close(up);
    }

    return 0;
}

up.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <string.h>
int main(char argc,char* argv[])
{
    
    char buf[255];
    int fd=atoi(argv[1]);
    printf("%d\n",fd);
    int up=atoi(argv[2]);
    printf("%d\n",up);
    size_t size=atoi(argv[3]);
    printf("%ld\n",size);
    int flag=atoi(argv[4]);
    printf("%d\n",flag);
    lseek(fd,0,SEEK_SET);
    lseek(up,0,SEEK_SET);
    for (size_t i = 0; i < size / 2 + flag; i++) // 父进程复制上半截,某些地方没必要判断
        {
            read(fd, buf, 1);
            write(up, buf, 1);
            memset(buf, 0, sizeof(buf));
        }
    printf("前半结束\n");
    close(fd);
    close(up);
    return 0;
}

down.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <string.h>
int main(char argc, char *argv[])
{

    char buf[255];
    int fd = atoi(argv[1]);
    printf("%d\n", fd);
    int up = atoi(argv[2]);
    printf("%d\n", up);
    size_t size = atoi(argv[3]);
    printf("%ld\n", size);
    int flag = atoi(argv[4]);
    printf("%d\n", flag);
    lseek(fd, size / 2, SEEK_SET);
    lseek(up, size / 2, SEEK_SET);
    for (size_t i = 0; i < size / 2 + flag; i++)
    {
        memset(buf, 0, sizeof(buf));
        if (read(fd, buf, 1) < 0)
        {
            perror("read");
            break;
        }
        write(up, buf, 1);
    }
    //printf("前半结束\n");
    close(fd);
    close(up);
    return 0;
}

 

2.僵尸进程,孤儿进程,守护进程

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
    pid_t pid=fork();
    if(pid<0)
    {
        perror("fork");
        return -1;
    }
    if(pid==0)
    {
        exit(0);//僵尸进程
    }
    sleep(1);
    while (1)
    {
        /* code */
    }
    
    return 0;
}

 

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
    pid_t pid=fork();
    if(pid<0)
    {
        perror("fork");
        return -1;
    }
    if(pid==0)//孤儿进程
    {
        while (1)
        {
            /* code */
        }
        
    }
    return 0;
}

 

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
int main(char argc, char *argv[])
{
    int fd;
    pid_t pid = fork();
    if (pid < 0)
    {
        perror("fork");
        return -1;
    }
    if (pid == 0)
    {
        setsid();
        chdir("/");
        umask(0);
        for (int i = 0; i < getdtablesize(); i++)
        {
            close(i);
        }
        int flag = 1;
        while (1)
        {

            // chdir("/home/gec/gec6818/temp/进程/1.txt");
            // system("rm 1.txt");
            if ((fd = open("/home/gec/gec6818/temp/进程/1.txt", O_WRONLY | O_APPEND)) < 0)
            {
                perror("open");
                return -1;
            }
            write(fd, "adsd", 3);
            flag++;
            sleep(2);
        }
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值