操作系统

操作系统作业

思考题_父进程与子进程

root@LAPTOP-EIHA6FN9:/home/hyx/cpp# vim test.c
root@LAPTOP-EIHA6FN9:/home/hyx/cpp# vim test1.c
root@LAPTOP-EIHA6FN9:/home/hyx/cpp# gcc test1 -lm -o test1
gcc: fatal error: input file ‘test1’ is the same as output file
compilation terminated.
root@LAPTOP-EIHA6FN9:/home/hyx/cpp# ./test1
children1 process:B
father process:A
children2 process:C
root@LAPTOP-EIHA6FN9:/home/hyx/cpp# 
#include <stdio.h>
#include <unistd.h>
int main(void) {
        int val1=fork();// your code goes here
        int val2=fork();
        if(val1==0 && val2!=0)
                printf("children1 process:B\n");
        if(val2==0 && val1!=0)
                printf("children2 process:C\n");
        if(val1!=0 && val2!=0)
                printf("father process:A\n");
        return 0;
}

普通题__linux线程

源代码:

#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
#define Num_Of_TH 10
void *print_hw(void *tid)
{
    printf("Hello Wolrd. Thread ID:%d\n",tid);
    pthread_exit(NULL);
}
int main(int argc,char *agrv[])
{
    pthread_t threads[Num_Of_TH];
    int status;
    for(int i=0;i<Num_Of_TH;++i){
        printf("Main here.Creating thread %d\n",i);
        status=pthread_create(&threads[i],NULL,print_hw,(void *)i);
        if(status!=0){
            printf("Oops,pthread_create returned error code %d\n",status);
            exit(-1);
        }
    }
    exit(NULL);
}

ubuntu20.04:

image-20210312233548388

root@LAPTOP-EIHA6FN9:/home/hyx/cpp# gcc test2.c -o test2 -lpthread
test2.c: In function ‘print_hw’:
test2.c:7:37: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘void *’ [-Wformat=]
    7 |     printf("Hello Wolrd. Thread ID:%d\n",tid);
      |                                    ~^    ~~~
      |                                     |    |
      |                                     int  void *
      |                                    %p
test2.c: In function ‘main’:
test2.c:16:57: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   16 |         status=pthread_create(&threads[i],NULL,print_hw,(void *)i);
      |                                                         ^
test2.c:22:10: warning: passing argument 1 of ‘exit’ makes integer from pointer without a cast [-Wint-conversion]
   22 |     exit(NULL);
      |          ^~~~
      |          |
      |          void *
In file included from test2.c:3:
/usr/include/stdlib.h:617:23: note: expected ‘int’ but argument is of type ‘void *’
  617 | extern void exit (int __status) __THROW __attribute__ ((__noreturn__));
      |                   ~~~~^~~~~~~~
root@LAPTOP-EIHA6FN9:/home/hyx/cpp# ./test2
Main here.Creating thread 0
Main here.Creating thread 1
Hello Wolrd. Thread ID:0
Main here.Creating thread 2
Hello Wolrd. Thread ID:1
Main here.Creating thread 3
Hello Wolrd. Thread ID:2
Main here.Creating thread 4
Hello Wolrd. Thread ID:3
Main here.Creating thread 5
Hello Wolrd. Thread ID:4
Main here.Creating thread 6
Hello Wolrd. Thread ID:5
Main here.Creating thread 7
Hello Wolrd. Thread ID:6
Main here.Creating thread 8
Hello Wolrd. Thread ID:7
Main here.Creating thread 9
Hello Wolrd. Thread ID:8
Hello Wolrd. Thread ID:9

ubuntu16.04:

oot@ubuntu:/home/hadoop/桌面# gcc test2.c -o test2 -lpthread
test2.c: In function ‘print_hw’:
test2.c:7:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘void *’ [-Wformat=]
     printf("Hello Wolrd. Thread ID:%d\n",tid);
            ^
test2.c: In function ‘main’:
test2.c:16:57: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
         status=pthread_create(&threads[i],NULL,print_hw,(void *)i);
                                                         ^
test2.c:22:10: warning: passing argument 1 of ‘exit’ makes integer from pointer without a cast [-Wint-conversion]
     exit(NULL);
          ^
In file included from test2.c:3:0:
/usr/include/stdlib.h:543:13: note: expected ‘int’ but argument is of type ‘void *’
 extern void exit (int __status) __THROW __attribute__ ((__noreturn__));
             ^
root@ubuntu:/home/hadoop/桌面# ./test2
Main here.Creating thread 0
Main here.Creating thread 1
Main here.Creating thread 2
Main here.Creating thread 3
Main here.Creating thread 4
Hello Wolrd. Thread ID:0
Main here.Creating thread 5
Main here.Creating thread 6
Hello Wolrd. Thread ID:1
Hello Wolrd. Thread ID:2
Hello Wolrd. Thread ID:3
Hello Wolrd. Thread ID:4
Hello Wolrd. Thread ID:5
Main here.Creating thread 7
Main here.Creating thread 8
Main here.Creating thread 9
Hello Wolrd. Thread ID:6
Hello Wolrd. Thread ID:7
Hello Wolrd. Thread ID:8

linux网上编译器:

image-20210312234123503

线程无输出,不知道为什么。。

posted @ 2021-03-08 15:11  祸国雨曦  阅读(76)  评论(0)    收藏  举报