操作系统学习(Linux) 多进程和多线程 C语言

Linux进程创建和撤销

系统:Linux Ubuntu          编译器:gcc

编译命令:gcc -o 指定文件名 文件

eg(以源代码文件名为 opsysExep1 为例):

gcc -o opsysExep1 opsysExep1.cpp

执行命令: ./文件名

eg:

./opsysExep1

PS:要编译运行的源代码一定是当前目录,不在当前目录的话先cd到所在目录。

Linux环境下用C语言编写程序,创建多个进程(模拟),过程:

1.写函数Createprocess()创建两个子进程process2和process3,写函数childfun()来判断是几号进程,并在函数Createprocess()中调用childfun()来进行哪个子进程的操作。

2.写函数process2()实现对系统命令的调用执行,例如ls,ps,free等;写函数process3()创建5、6号进程,写函数fun()来判断是几号进程,并在函数process3 ()中调用fun()来进行哪个子进程的操作。

3.写函数process4()创建两个线程Thread1,Thread2。

4.写函数process5()执行一个用户写的可执行文件:输出“hello,world!”;写函数Thread1()执行求(1~n)之间的素数;写函数Thread2()执行生成Fibonacci序列,并写函数Fibonacci()求值,在Thread2()中调用Fibonacci()。

遇到的问题(踩过的坑)

1.调用pthread_create()函数时,根据函数的参数,第三个参数的取值错误。

如图所示

解决方法:

调用的线程函数写成void *Thread1(void* arg)

这个看具体情况,在调用时用的是pthread_create(&mthread1,NULL,Thread1,NULL);所以线程函数命名应该写void *Thread1(void* arg)

也可以使用void Thread1(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;
}

运行结果:

发布了1 篇原创文章 · 获赞 0 · 访问量 6

猜你喜欢

转载自blog.csdn.net/lQreye/article/details/104972285