操作系统 安装gcc,完成hello.c,fork.c,processes.c,communication.c

今日作业如下:
在这里插入图片描述

安装gcc

首先先检查一下是否有gcc(虽然一般都没有)

gcc --version

然后就会报错(啊好还没装呢)
然后输入

sudo apt-get install build-essential

在这里插入图片描述
再输入一次查看

gcc --version

在这里插入图片描述

hello.c

新建一个文件夹放作业

mkdir CaoZuoXiTong
cd CaoZuoXiTong

在这里插入图片描述
输入进入hello.c进行编辑

vim hello.c

输入i进入insert模式写入代码:

#include <stdio.h>
int main(){
    
    
        printf("Hello World!skeptical");
        return 0;

}

然后按esc键和shift+键输入wq保存.
输入

gcc hello.c -o hello
./hello

在这里插入图片描述

fork.c

在终端中输入vim fork.c
同样输入

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

int main(){
    
    
    pid_t pid;
    pid = fork();
    if(pid < 0) {
    
    
        fprintf(stderr, "Fork Failed");
        exit(-1);
    }
    else if (pid == 0) {
    
    
        execlp("/bin/ls","ls",NULL);
    }
    else {
    
    
        wait(NULL);
        printf("Child Complete\n");
        exit(0);
    }
}

保存退出后再在终端中输入

gcc fork.c -o fork
./fork

在这里插入图片描述

processes.c

继续

vim processes.c

输入

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

int value;
int main(){
    
    
    pid_t pid;
    pid = fork();
    if (pid < 0){
    
    
        fprintf(stderr, "Fork Failed");
        exit(-1);
    }
    else if (pid == 0) {
    
    
        for(;;){
    
    
            printf("In child process, value is %d\n", value++);
            printf("In child process, value's address is %p\n", &value);
            sleep(1);
        }
    }
    else {
    
    
        for(;;){
    
    
            printf("In parent process, value is %d\n", value);
            printf("In parent process, value's address is %p\n",&value);
            sleep(1);
        }
    }
}

在这里插入图片描述
可以用ctrl+c中断

communication.c

输入

vim  communication.c

输入

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

void SignHandler1(int iSignNo);
void SignHandler2(int iSignNo);
int child1,child2;
int filedis[2];

int main(void){
    
    
    char buffer[40];
    char info[40];
    int status;
    int counter = 1;
    printf("Process Parent pid %d\n",getpid());
    //创建无名管道
    if(pipe(filedis) < 0){
    
    
        printf("Create pipe failed\n");
        return -1;
    }
    //设置软中断信号SIGINT
    signal(SIGINT,SignHandler1);
    //创建进程1,2
    child1 = fork();
    printf("child1=%d\n", child1);
    if(child1 == 0){
    
            //子进程1
        printf("Process1 pid %d\n",getpid());
        signal(SIGINT,SIG_IGN);
        signal(SIGUSR1,SignHandler2);
        while(1)
        {
    
    
            close(filedis[0]);
            sprintf(info,"I send you %d times",counter);
            write(filedis[1],info,30);
            counter++;
            printf("PID: %d, I have sent.\n", getpid());
            sleep(1);
                    }
    }
    else if(child1 > 0){
    
    
        child2 = fork();
        if(child2 == 0){
    
    
            printf("Process2 pid %d\n",getpid());
            signal(SIGINT,SIG_IGN);
            signal(SIGUSR1,SignHandler2);
            while(1){
    
    
                close(filedis[1]);
                read(filedis[0],buffer,40);
                printf("PID: %d, I received: %s\n",getpid(),buffer);
            }
        }
        //等待进程1,2退出
        waitpid(child1,NULL,0);
        printf("Child Process1 is over\n");
        waitpid(child2,NULL,0);
        printf("Child Process1 is over\n");
        //关闭管道
        close(filedis[0]);
        close(filedis[1]);
        printf("Parent Process is killed!\n");
    }
    return 0;
}

void SignHandler1(int iSignNo){
    
    
    printf("\nParent receive signal Ctrl+C\n");
    if(iSignNo == SIGINT){
    
    
        kill(child1,SIGUSR1);
        kill(child2,SIGUSR1);
    }
}

void SignHandler2(int iSignNo){
    
    
    close(filedis[0]);
    close(filedis[1]);
    if(child1 == 0 && iSignNo == SIGUSR1){
    
    
        printf("Child Process1 is killed by Parent!\n");
        exit(0);
    }
    if(child2 == 0 && iSignNo == SIGUSR1){
    
    
         printf("Child Process2 is killed by Parent!\n");
        exit(0);
    }
}

输入

gcc communication.c -o communication
./communication

在这里插入图片描述


完成啦,今天又买了炭烧,这次是胀气debuff,大无语。
陈老师真的好可爱,下课还嘟哝位子上蚊子多。

猜你喜欢

转载自blog.csdn.net/weixin_43820665/article/details/109075698
今日推荐