进程和线程编程之二

【代码1】test_mutex.c

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <semaphore.h>
#include <unistd.h>
#include <pthread.h>

int global1 = 0;
int global2 = 0;

pthread_mutex_t mutex;

void *thread1(void *arg)
{
	while(1) {
#ifdef _MUTEX_LOCK_
		pthread_mutex_lock(&mutex);
#endif
		global1++;
		global2++;
		if (global1 != global2)
			printf("in thread1, global1 = %d, global2 = %d\n", global1, global2);
#ifdef _MUTEX_LOCK_
		pthread_mutex_unlock(&mutex);
#endif		
	}
	pthread_exit(NULL);
}

void *thread2(void *arg)
{
	while(1) {
#ifdef _MUTEX_LOCK_		
		pthread_mutex_lock(&mutex);
#endif
		global1--;
		global2--;
		if (global1 != global2)
			printf("in thread2, global1 = %d, global2 = %d\n", global1, global2);
#ifdef _MUTEX_LOCK_
		pthread_mutex_unlock(&mutex);
#endif
	}

	pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
	pthread_t tid1, tid2;
	pthread_mutex_init(&mutex, NULL);
	
	if (0 > pthread_create(&tid1, NULL, thread2, NULL)) {
		fprintf(stderr, "pthread_create : %s\n", strerror(errno));
		exit(1);
	}
	
	if (0 > pthread_create(&tid2, NULL, thread1, NULL)) {
		fprintf(stderr, "pthread_create : %s\n", strerror(errno));
		exit(1);
	}
	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);
	exit(0);
}

 

【代码2】test_pthread_attr_t.c

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>

void thread1(void *arg)
{
	int i;
	printf("in thread1, tid is %lx\n", pthread_self());
	
	for (i = 0; i < 100; i++) {
		printf("this is in thread1...\n");
		sleep(1);
	}
	pthread_exit(NULL);
}

void thread2(void *arg)
{
	int i;
	printf("in thread2, tid is %lx\n", pthread_self());
	sleep(2);
	for (i = 0; i < 4; i++) {
		printf("this is in thread2...\n");
		sleep(1);
	}
	pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
	pthread_t tid1, tid2;
	pthread_attr_t attr;	
	pthread_attr_init(&attr);
	pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	if (0 != pthread_create(&tid1, &attr, (void *)thread1, NULL)) {
		fprintf(stderr, "pthread_create : %s\n", strerror(errno));
		exit(1);
	}

	if (0 != pthread_create(&tid2, NULL, (void *)thread2, NULL)) {
		fprintf(stderr, "pthread_create : %s\n", strerror(errno));
		exit(1);
	}

	pthread_join(tid1, NULL);
	printf("after pthread_join(tid1, NULL);...\n");
	pthread_join(tid2, NULL);
	printf("after pthread_join(tid2, NULL);...\n");
	return 0;
}

 

【代码3】test_pthread_create.c

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>

int val;
void *thread1(void *arg)
{
	printf("in thread1, arg is %d\n", (int)arg);
	//val = 10;
	pthread_exit((void *)100);
}

void *thread2(void *arg)
{
	printf("in thread2, arg is %d\n", (int)arg);
	//val = 10;
	pthread_exit((void *)500);
}

int main(int argc, char *argv[])
{
	pthread_t tid1, tid2;
	if (0 > pthread_create(&tid1, NULL, thread1, (void *)2000)) {
		fprintf(stderr, "pthread_create : %s\n", strerror(errno));
		exit(1);
	}

	if (0 > pthread_create(&tid2, NULL, thread2, (void *)3000)) {
		fprintf(stderr, "pthread_create : %s\n", strerror(errno));
		exit(1);
	}

	int exit_code;
	
	pthread_join(tid1, (void **)&exit_code);
	printf("exit_code of thread1 is %d\n", exit_code);

	pthread_join(tid2, (void **)&exit_code);
	printf("exit_code of thread2 is %d\n", exit_code);
	exit(0);
}

 

【代码4】test_pthread_exit.c

 

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>


void thread(void *i)
{
	int return_value = 10;
	printf("in thread, return value is %d\n", return_value);
	pthread_exit((void *)return_value);
}


int main(void)
{
	pthread_t tid;
	int a = 10;
	int status;
	
	if (0 > pthread_create(&tid, NULL, (void *)thread, (void *)a)) {
		fprintf(stderr, "pthread_create : %s\n", strerror(errno));
		exit(1);
	}
	
	printf("in process, thread id is %lu\n", tid);
	pthread_join(tid, (void **)&status);
	printf("in process, the return value from thread is %d\n", status);
	//while(1);
	exit(0);
	
}

 

【代码5】test_sem.c

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <semaphore.h>
#include <unistd.h>
#include <pthread.h>


sem_t sem1;
sem_t sem2;

void *hello(void *arg)
{
	int k;
	while(1) {
		sem_wait(&sem1);
		sem_getvalue(&sem1, &k);
		printf("in hello, sem1 is %d\n", k);
		printf("hello ");
		sem_post(&sem2);
	}

	pthread_exit(NULL);
}

void *world(void *arg)
{
	int k;
	while(1) {
		sem_wait(&sem2);
		sem_getvalue(&sem2, &k);
		printf("world\n");
		printf("in world, sem2 is %d\n", k);
		sleep(1);
		sem_post(&sem1);
	}
	pthread_exit(NULL);
}


int main(int argc, char *argv[])
{
	pthread_t tid1, tid2;
	sem_init(&sem1, 0, 1);
	sem_init(&sem2, 0, 0);
	if (0 > pthread_create(&tid1, NULL, hello, NULL)) {
		fprintf(stderr, "pthread_create : %s\n", strerror(errno));
		exit(1);
	}

	if (0 > pthread_create(&tid2, NULL, world, NULL)) {
		fprintf(stderr, "pthread_create : %s\n", strerror(errno));
		exit(1);
	}

	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);
	exit(0);
}

 

【代码6】test_thread_process.c

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>


void printfid(char *s)
{
	printf("%s, pid is %d, tid is %lx\n", s, getpid(), pthread_self());
}

void *thread_fun(void *arg)
{
	//char *buf = "";
	printfid("in thread_fun : ");
	pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
	pthread_t tid;
	void *status;
	if (0 > pthread_create(&tid, NULL, thread_fun, NULL)) {
		fprintf(stderr, "pthread_create : %s\n", strerror(errno));
		exit(1);
	}
	
	pthread_join(tid, NULL);
	printfid("in main :");
	exit(0);
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百里杨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值