linux c语言线程(linux 查看线程)
在linux下c语言编程有关进程的问题
不太了解c的多进程(多线程?)编程,没看懂这个程序,我猜原因可能有:
进程(线程)是无序进行的
printf有输出缓存(就是使用printf输出,不一定会立即输出)
相关资料:
printf输出函数,每执行一个printf输出函数,输出的数不是“肯定立刻”打印到屏幕上的,只有遇到一下几种情况时,printf输出的数据(执行了printf,但还没有打印到屏幕的数据)才会全部打印到屏幕上:
1、有输入请求的时候,会立马输出到屏幕
2、输出有换行符的时候,也会马上输出到屏幕上
3、程序结束的时候也会马上输出到屏幕上
4、输出缓冲区满的时候
c语言实例,linux线程同步的信号量方式 谢谢
这么高的悬赏,实例放后面。信号量(sem),如同进程一样,线程也可以通过信号量来实现通信,虽然是轻量级的。信号量函数的名字都以"sem_"打头。线程使用的基本信号量函数有四个。
信号量初始化。
intsem_init(sem_t*sem,intpshared,unsignedintvalue);
这是对由sem指定的信号量进行初始化,设置好它的共享选项(linux只支持为0,即表示它是当前进程的局部信号量),然后给它一个初始值VALUE。
等待信号量。给信号量减1,然后等待直到信号量的值大于0。
intsem_wait(sem_t*sem);
释放信号量。信号量值加1。并通知其他等待线程。
intsem_post(sem_t*sem);
销毁信号量。我们用完信号量后都它进行清理。归还占有的一切资源。
intsem_destroy(sem_t*sem);
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#include<errno.h>
#definereturn_if_fail(p)if((p)==0){printf("[%s]:funcerror!/n",__func__);return;}
typedefstruct_PrivInfo
{
sem_ts1;
sem_ts2;
time_tend_time;
}PrivInfo;
staticvoidinfo_init(PrivInfo*thiz);
staticvoidinfo_destroy(PrivInfo*thiz);
staticvoid*pthread_func_1(PrivInfo*thiz);
staticvoid*pthread_func_2(PrivInfo*thiz);
intmain(intargc,char**argv)
{
pthread_tpt_1=0;
pthread_tpt_2=0;
intret=0;
PrivInfo*thiz=NULL;
thiz=(PrivInfo*)malloc(sizeof(PrivInfo));
if(thiz==NULL)
{
printf("[%s]:Failedtomallocpriv./n");
return-1;
}
info_init(thiz);
ret=pthread_create(&pt_1,NULL,(void*)pthread_func_1,thiz);
if(ret!=0)
{
perror("pthread_1_create:");
}
ret=pthread_create(&pt_2,NULL,(void*)pthread_func_2,thiz);
if(ret!=0)
{
perror("pthread_2_create:");
}
pthread_join(pt_1,NULL);
pthread_join(pt_2,NULL);
info_destroy(thiz);
return0;
}
staticvoidinfo_init(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
thiz->end_time=time(NULL)+10;
sem_init(&thiz->s1,0,1);
sem_init(&thiz->s2,0,0);
return;
}
staticvoidinfo_destroy(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
sem_destroy(&thiz->s1);
sem_destroy(&thiz->s2);
free(thiz);
thiz=NULL;
return;
}
staticvoid*pthread_func_1(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
while(time(NULL)<thiz->end_time)
{
sem_wait(&thiz->s2);
printf("pthread1:pthread1getthelock./n");
sem_post(&thiz->s1);
printf("pthread1:pthread1unlock/n");
sleep(1);
}
return;
}
staticvoid*pthread_func_2(PrivInfo*thiz)
{
return_if_fail(thiz!=NULL);
while(time(NULL)<thiz->end_time)
{
sem_wait(&thiz->s1);
printf("pthread2:pthread2gettheunlock./n");
sem_post(&thiz->s2);
printf("pthread2:pthread2unlock./n");
sleep(1);
}
return;
}
Linux系统中一般使用什么语言编程呀
Linux操作系统是用C语言、汇编语言编写的。
主要是C,C是Linux的“母语”,这也是linux这个开源环境和本身机制所导致的,就连linus都力挺C,而驳斥C++。虽然没必要拒绝C++,但是,不可否认,C更适合linux~。
Linux操作系统主要包括内核和组件系统。Linux内核大部分是用C语言编写的,还有部分是用汇编语言写的,因为在对于硬件上,汇编有更好的性能和速度。
Linux的一些组件系统和附加应用程序是用C、C++、Python、perl等语言写的。
扩展资料:
Linux的基本思想有两点:
第一,一切都是文件;
第二,每个软件都有确定的用途。其中第一条详细来讲就是系统中的所有都归结为一个文件,包括命令、硬件和软件设备、操作系统、进程等等对于操作系统内核而言,都被视为拥有各自特性或类型的文件。至于说Linux是基于Unix的,很大程度上也是因为这两者的基本思想十分相近。
参考资料来源:百度百科-linux系统