【操作系统】多线程和多进程的管理

一、实验目的

加深对进程概念的理解,明确进程与线程的区别。
掌握Linux进程创建和撤销的方法,进一步认识并发执行的实质。
了解多线程的程序设计方法。

二、实验项目内容

多进程和多线程
 1号进程创建2,3号两个进程
 2号进程创建两个线程Thread1,Thread2
 Thread1:求(1~n)之间的素数
 Thread2:生成Fibonacci序列
 3号进程创建4,5号两个进程
 4号进程执行系统命令,ls,ps,cp等
 5号进程执行一个用户编写的可执行文件
 每个进程输出自己的进程ID和父进程的进程ID,观察分析,并画出程序的进程树结构。

三、实验程序代码:
#include <stdio.h>
#include <math.h>
#include <sys/types.h>  
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

/**
* Attention to add -lpthread and -lm when compile
* eg : gcc process.c -lpthread -lm -o process
*/

//The thread to find the prime number between (1~n)
void *MyThread1(void* arg)
{
    int n;
    //Get the resolve range
    printf("input the value of n:\n");
    scanf("%d",&n);
    printf("the prime number between 1~%d is:\n",n);

    for(int m=2;m<=n;m++)
    {
        if (m<=3)
        {
            printf("%4d",m);
            continue;  
        }

       int k=sqrt(m);  
       for(int i=2; i<=k; i++)
       {
            // printf("m: %f\n",k);
            if(m%i==0) 
                break;  
            if(i>=k) 
                printf("%4d",m);  
       }
    }

    printf("\n thread1 exit!\n");
    pthread_exit(0);
}

//The thread to generate Fibonacci sequences
void *MyThread2(void* arg)
{
    int fib0=0,fib1=1,fib2,i,N;
    printf("input fib value N:\n");
    scanf("%d",&N);
    printf("the fib sequences as following:\n");
    for (int i = 0; i < N; i++)
    {
        if (i==0)
        {
            printf("0");
        }
        else if (i==1)
        {
            printf("1");
        }
        else
        {
            fib2=fib0+fib1;
            printf("%d",fib2 );
            fib0=fib1;
            fib1=fib2;
        }
    }
    printf("\n thread2 exit!\n");
    pthread_exit(0);
}

//No.2 process create two threads
int createThread()
{
    int ret1=0,ret2=0;
    //Define the id of thread
    pthread_t id1,id2;
    //Create thread1
    ret1=pthread_create(&id1,NULL,MyThread1,NULL);
    if (ret1)
    {
        printf("Create pthread error!\n");
        return 1;
    }
    //Create thread2
    ret2=pthread_create(&id2,NULL,MyThread2,NULL);
    if (ret2)
    {
        printf("Create pthread error\n");
        return 1;
    }

    //Mainthread wait for childthread
    pthread_join(id1,NULL);
    pthread_join(id2,NULL);

    printf("main thread exit!\n");
    return 0;
}

//The Function of N0.4 and No.5
void childProcessfunc(int i)
{
    switch(i)
    {
        case 4:
            printf("\nThis is NO.4 process, ID is  %d, parent ID is %d, will create 2 thread\n",getpid(),getppid());
            printf("\nExcute system command ls:\n");
            system("ls");
            printf("\nExcute system command ps:\n");
            system("ps");
            printf("\nExcute system command cp:\n");
            system("cp sayhello.c sayhello.cpp");

            break;
        case 5:
            printf("\nThis is NO.5 process, ID is  %d, parent ID is %d, will create 2 thread\n",getpid(),getppid());
            printf("\nCarray out executable program:\n");
            system("./sayhello");
            break;
    }
    exit(0);
}

//No.3 process create two child process, N0.4 and No.5
int createProcess()
{
    int i;
    for (int i = 4; i <= 6; i++)
    {
        pid_t child=fork();
        //create process failed
        if (child==-1)    
        {
            printf("Error hanppened in fork function!\n");
            return 0;
        }
        //exert function when success
        else if(child==0)
        {
            childProcessfunc(i);
        }
    }

    for (int i = 0; i < 2; i++)
    {
        //Parent process waits child process
        pid_t tempPid=wait(NULL);
        printf("The process %d exit\n", tempPid);
    }

    return 0;
}

//The Function of N0.2 and No.3
void mainProcessfunc(int i)
{
    switch(i)
    {
        case 2:
            printf("\nThis is NO.2 process, ID is  %d, parent ID is %d, will create 2 thread\n",getpid(),getppid());
            createThread();
            break;
        case 3:
            printf("\nThis is NO.3 process, ID is  %d, parent ID is %d, will create 2 thread\n",getpid(),getppid());
            createProcess();
            break;
    }
    exit(0);
}

int main()
{
    int i;
    //Create two child process, No.2 and No.3
    for (int i = 2; i < 4; i++)
    {
        pid_t child=fork();
        if (child==-1)
        {
            printf("Error happened in fork function!\n");
            return 0;
        }
        else if(child==0)
        {
            // printf("process Id is %d:\n",getpid());
            mainProcessfunc(i);
        }
    }
    for (int i = 0; i < 2; i++)
    {
        //Parent process waits child process
        pid_t tempPid=wait(NULL);
        printf("\nThe process %d exit\n", tempPid);
    }
    //Root process exit
    printf("\nThe No.1 root process Id is %d exit\n",getpid());

    return 0;
}
注:在编译命令中添加-lpthread和 -lm ,-lpthread是表示要连接到pthread的库是这里省略的lib,因为在求素数算法中使用math库中sqrt函数,需要用到math.h,则加上 -lm。
四、实验结果

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序猿周周

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值