Linuxプログラミング問題
25751 ワード
1.親プロセスに2つのサブプロセスがあり、この2つのサブプロセスにはそれぞれ2つのサブプロセスがあり、各プロセスは5秒休止した後に終了し、終了する前に自分のプロセスid番号を印刷します.
2.read関数を使用してソースファイルsc 1からプログラムを設計する.Dataデータを読み出し、write関数でターゲットファイルに書き込み、ターゲットファイル名はキーボードで入力します.また、ターゲットファイルの内容を出力します.
3.反発ロックによるpthread_mutex_tは,反発して臨界リソースにアクセスするマルチスレッドプログラムを記述する.プログラム完了の機能要件:主関数は共有メモリ変数mv(初期値は10)を初期化し、反発ロックを作成し、2つのスレッドを作成し、2つのスレッドの実行が完了するのを待ってから、mvの値を出力し、反発ロックを解放する.2つのスレッドは、それぞれ、反発ロックを取得することによってメモリ変数mvを10回、5回自己減算する機能を実現する.
4.プログラミングは、10個の100以内の整数をランダムに生成し、そのうち7で割り切れる数を出力する.Makefileファイルを作成します.(2つの方法)
転載先:https://www.cnblogs.com/yyphappness/p/11171815.html
1 # include
2 # include
3 # include
4 int main()
5 {
6 int reg,i;
7 for(i=0;i<2;i++)
8 {
9 reg=fork(); if(reg==0) break;
10 }
11 if(reg==0)
12 {
13 for(i=0;i<2;i++)
14 {
15 reg=fork();
16 if(reg==0) break;
17 }
18 }
19 sleep(5);
20 printf("this is %d process
", getpid());
21 }
2.read関数を使用してソースファイルsc 1からプログラムを設計する.Dataデータを読み出し、write関数でターゲットファイルに書き込み、ターゲットファイル名はキーボードで入力します.また、ターゲットファイルの内容を出力します.
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 int main()
9 {
10 int fdsrc,fddes,nbytes;
11 int flags=0_CREAT | 0_TRUNC | 0_WRONLY;
12 int z;
13 char buf[20],src[20],des[20];
14 printf(" :");
15 scanf("%s",des);
16 fdsrc=open("sc1.dat",0_RDONLY);
17 if(fdsrc<0)
18 {
19 exit(1);
20 }
21 fddes=open(des,flags,0644);
22 if(fddes<0)
23 {
24 exit(1);
25 }
26 while((nbytes=read(fdsrc,buf,20))>0) // read
27 {
28 z=write(fddes,buf,nbytes);// write
29 if(z<0)
30 {
31 perror(" ");
32 }
33 }
34 close(fdsrc);
35 close(fddes);
36 printf(" !");
37
38 //
39 int i;
40 fddes=open(des,0_RDONLY);
41 if(fddes<0)
42 {
43 exit(1);
44 }
45 while((nbytes=read(fddes,buf,20))>0)
46 {
47 for(i=0;i<sizeof(buf);i++)
48 {
49 printf("%c",buf[i]);
50 }
51 }
52 close(fddes);
53 exit(0);
54 }
3.反発ロックによるpthread_mutex_tは,反発して臨界リソースにアクセスするマルチスレッドプログラムを記述する.プログラム完了の機能要件:主関数は共有メモリ変数mv(初期値は10)を初期化し、反発ロックを作成し、2つのスレッドを作成し、2つのスレッドの実行が完了するのを待ってから、mvの値を出力し、反発ロックを解放する.2つのスレッドは、それぞれ、反発ロックを取得することによってメモリ変数mvを10回、5回自己減算する機能を実現する.
1 #include
2 #include
3 #include
4 #include
5
6 // mv
7 int mv=10;
8 //
9 pthread_mutex_t mutex;
10 //
11 static void pthread_funAdd(void);//
12 static void pthread_funSub(void);//
13 //
14 int main(void)
15 {
16 //
17 pthread_t pt_Add=0; //
18 pthread_t pt_Sub=0; //
19 int ret=0;
20 //
21 pthread_mutex_init(&mutex,NULL);
22 // Add、Sub
23 ret=pthread_create(&pt_Add, //
24 NULL, //
25 (void*)pthread_funAdd, //
26 NULL); //
27 if(ret != 0)
28 {
29 perror("pthread_Add_create");
30 }
31
32 ret=pthread_create(&pt_Sub, //
33 NULL, //
34 (void*)pthread_funSub, //
35 NULL); //
36 if(ret != 0)
37 {
38 perror("pthread_Sub_create");
39 }
40 // Add、Sub
41 pthread_join(pt_Add,NULL);
42 pthread_join(pt_Sub,NULL);
43 printf("main program exit!
");
44 return 0;
45 }
46
47 // Add
48 static void pthread_funAdd(void)
49 {
50 int i=0;
51 pthread_mutex_lock(&mutex); //
52 sleep(1);
53 printf("This is pthread_Add!
");
54 // 10
55 for(i=0;i<10;i++)
56 {
57 mv++;
58 }
59 printf("pthread_Add 10 to num:%d
",mv);
60 pthread_mutex_unlock(&mutex); //
61 pthread_exit(NULL);
62 }
63
64 // Sub
65 static void pthread_funSub(void)
66 {
67 int i=0;
68 pthread_mutex_lock(&mutex); //
69 sleep(1);
70 printf("This is pthread_Sub
!
");
71 // 5
72 for(i=0;i<5;i++)
73 {
74 mv--;
75 }
76 printf("pthread_Sub 5 to num:%d
",mv);
77 pthread_mutex_unlock(&mutex); //
78 pthread_exit(NULL);
79 }
4.プログラミングは、10個の100以内の整数をランダムに生成し、そのうち7で割り切れる数を出力する.Makefileファイルを作成します.(2つの方法)
1 #include
2 #include
3 #include
4 //
5 int main()
6 {
7 int i,j;
8 for(i=0;i<10;i++)
9 {
10 j=1+(int)(100.0*rand()/(RAND_MAX+1.0));
11 if(j%7==0)
12 {
13 printf("%d 7
");
14 }
15 }
16 }
17
18 MakeFile :
19 testrand:main.o
20 gcc main.o -o testrand
21 main.o:main.c
22 gcc main.c -o main.o
1 #include
2 #include
3 #include
4
5 int main()
6 {
7 int a[10],i;
8 srand((unsigned int)time(NULL));
9 for(i=0;i<10;i++)
10 {
11 a[i]=rand()%100+1;
12 if(a[i]%7==0)
13 printf("ok :%d
",a[i]);
14 }
15 for(i=0;i<10;i++)
16 {
17 printf("%d ",a[i]);
18 }
19 printf("
");
20 return 0;
21 }
転載先:https://www.cnblogs.com/yyphappness/p/11171815.html