多进程和多线程编程

本文探讨了多进程和多线程编程的概念,详细阐述了线程同步与互斥的重要性,包括互斥锁的使用和条件变量的实现,为并发编程提供了关键知识。
摘要由CSDN通过智能技术生成

1、多进程编程

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
	pid_t child_pid;

	/* 创建一个子进程 */
	child_pid = fork();
	if(child_pid == 0)
	{
		printf("child pid\n");
		exit(0);
	}
	else
	{
		printf("father pid\n");
		sleep(60);
	}
	
	return 0;
}


 

2、多线程编程

#include <stdio.h>
#include <pthread.h>

struct char_print_params
{
	char character;
	int count;
};

void *char_print(void *parameters)
{
	struct char_print_params *p = (struct char_print_params *)parameters;
	int i;

	for(i = 0; i < p->count; i++)
	{
		fputc(p->character,stderr);
	}

	return NULL;
}

int main()
{
	pthread_t thread1_id;
	pthread_t thread2_id;
	struct char_print_params thread1_args;
	struct char_print_par
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值