Linuxプロセスの作成と失効
システム:Linux Ubuntuのコンパイラ:GCC
コンパイルコマンド:gccの-oファイル名を指定したファイル
例えば(例えばopsysExep1という名前のソース・コード・ファイル内):
gcc -o opsysExep1 opsysExep1.cpp
コマンドを実行します ./ファイル名を
例えば:
./opsysExep1
PS:ソースコードがディレクトリに最初のCD、その後、いない現在のディレクトリに、現在のディレクトリを実行している必要がありますコンパイルするには。
複数のプロセス(アナログ)、プロセスを作成し、C言語プログラムを使用してLinux環境を書きます:
1.書き込み機能のCreateProcess()は、二つのサブプロセスのプロセス2とprocess3、ライト機能childfunを(作成する)サブプロセスを操作するためのCreateProcessの処理及び呼び出しchildfun()関数を()を決定する日付です。
2.書き込み機能プロセス2などのように、LS、PS、自由となどのシステムコマンドの実装のための()の実装を呼び出し、;(、書き込み機能の楽しさを5,6プロセスを作成するための書込み機能process3は())プロセスを決定する日付と機能ですprocess3コール楽しい()()した子プロセスを操作します。
3.()関数のprocess4を書く二つのスレッドスレッド1、スレッド2が作成されます。
4.ライト機能process5()は、ユーザーが記述した実行ファイルを実行します。出力「こんにちは、世界!」;スレッド1ライト機能(実行要求(1〜n)の間の素数);スレッド2ライト機能()世代フィボナッチ数列を行います書き込み機能フィボナッチ()の評価は、中にスレッド2でフィボナッチを()()を呼び出します。
問題に遭遇する(ステップピット)
1.コールのpthread_create()関数、パラメータ、第三のパラメータの誤差値に応じて機能します。
図に示すように。
ソリューション:
スレッド1(void *型のarg)*無効書かれたスレッド関数呼び出し
これは、呼び出すときに使用され、特定の状況に依存のpthread_create(&mthread1、NULL、スレッド1、NULL);スレッド関数名を記述する必要がありますので、ボイド*スレッド1(void *型のarg)
また、ボイドスレッド1(void *型のarg)を使用することができますが、必要がコールにコードを変更したとき。
状況は未定義の関数であるとき2.プログラムが実行されます。
ソリューション:コンパイラのコマンドの実装の後ろに-lpthread追加
コード
#include<stdio.h>
#include<sys/types.h> //defind pid t
#include<unistd.h>
#include<sys/wait.h>
#include<pthread.h>
#include<stdlib.h>
void *Thread1(void* arg){
printf("This is thread1 of process4,fuction:Prime number\n");
int n = 0;
printf( "Enter a value :");
scanf("%d", &n);
int a = 0;
for(int i = 2; i < n; i++){
if(n%i == 0){
a++;
}
}
if(a==0){
printf("%dis Prime number\n", n);
}else{
printf("%dnot Prime number\n", n);
}
}
int Fibonacci(int n)
{ int N = 10007;
int Fn;
if (n==1 || n==2)
{
Fn=1;
}
else
{
Fn = (Fibonacci(n-1) + Fibonacci(n-2))%N;
}
return Fn;
}
void *Thread2(void* arg){
printf("This is thread2 of process4,fuction:Fibonacci\n");
int n = 0;
printf( "Enter a value :");
scanf("%d", &n);
int result;
result = Fibonacci(n);
printf("result = %d \n", result);
}
int process4(){
printf("This is process4,fuction:Create 2 pthread\n");
pthread_t mthread1, mthread2;
mthread1 = pthread_create(&mthread1,NULL,Thread1,NULL);
if(mthread1 != 0){
printf("Error happened in thread_create function\n");
return 1;
}
else( mthread1 == 0 );{
printf("create success\n");
}
mthread2 = pthread_create(&mthread2,NULL,Thread2,NULL);
if(mthread2 != 0){
printf("Error happened in thread_create function\n");
return 1;
}
else( mthread2 == 0 );{
printf("create success\n");
}
pthread_join(mthread1, NULL);
pthread_join(mthread2, NULL);
printf("main thread exit!\n");
return 0;
}
void process5(){
printf("This is process5,fuction:exe\n");
printf("Hello world\n");
}
void process2(){
printf("This is process2,fuction:system commend\n");
system("ls");
system("ps");
system("free");
}
void fun(int i)
{
switch(i)
{
case 4: process4();
break;
case 5: process5(); //printf hello world
break;
}
_exit(0);
}
int process3(){
printf("This is process3,fuction:create process4 and process5\n");
for(int i = 4; i <= 5; i++){
pid_t child;
child = fork();
if(child == -1){
printf("Error happened in fork function\n");
return 0;
}
else if( child == 0 ){
printf("create success\n");
fun(i);
}
}
for (int j = 0; j < 2;j++)
{
pid_t cpid = wait(NULL);
printf("the process % d exit \n", cpid);
}
//parent process exit
printf("The no.1 parent process ID is %d exit\n", getpid());
return 0;
}
void childfun(int i){
switch(i){
case 2 : process2();
case 3 : process3();
}
_exit(0);
}
int Createprocess(){
printf("Will Create processes...\n");
for(int i = 2; i <= 3; i++){
pid_t child;
child = fork();
if(child == -1){
printf("Error happened in fork function\n");
return 0;
}
else if( child == 0 ){
printf("create success\n");
childfun(i);
}
}
for (int j = 0; j < 2;j++)
{
pid_t cpid = wait(NULL);
printf("the process % d exit \n", cpid);
}
//parent process exit
printf("The no.1 parent process ID is %d exit\n", getpid());
return 0;
}
//main fuction
int main(){
Createprocess();
return 0;
}
結果: