
这么高的悬赏,实例放后面。信号量(sem),如同进程一样,线程也可以通过信号量来实现通信,虽然是轻量级的。信号量函数的名字都以"sem_"打头。线程使用的基本信号量函数有四个。
信号量初始化。int sem_init (sem_t *sem , int pshared, unsigned int value)
这是对由sem指定的信号量进行初始化,设置好它的共享选项(linux 只支持为0,即表示它是当前进程的局部信号量),然后给它一个初始值VALUE。
等待信号量。给信号量减1,然后等待直到信号量的值大于0。
int sem_wait(sem_t *sem)
释放信号量。信号量值加1。并通知其他等待线程。
int sem_post(sem_t *sem)
销毁信号量。我们用完信号量后都它进行清理。归还占有的一切资源。
int sem_destroy(sem_t *sem) #include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!/n", __func__)return}
typedef struct _PrivInfo
{
sem_t s1
sem_t s2
time_t end_time
}PrivInfo
static void info_init (PrivInfo* thiz)
static void info_destroy (PrivInfo* thiz)
static void* pthread_func_1 (PrivInfo* thiz)
static void* pthread_func_2 (PrivInfo* thiz)
int main (int argc, char** argv)
{
pthread_t pt_1 = 0
pthread_t pt_2 = 0
int ret = 0
PrivInfo* thiz = NULL
thiz = (PrivInfo* )malloc (sizeof (PrivInfo))
if (thiz == NULL)
{
printf ("[%s]: Failed to malloc priv./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)
return 0
}
static void info_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
}
static void info_destroy (PrivInfo* thiz)
{
return_if_fail (thiz != NULL)
sem_destroy (&thiz->s1)
sem_destroy (&thiz->s2)
free (thiz)
thiz = NULL
return
}
static void* pthread_func_1 (PrivInfo* thiz)
{
return_if_fail(thiz != NULL)
while (time(NULL) < thiz->end_time)
{
sem_wait (&thiz->s2)
printf ("pthread1: pthread1 get the lock./n")
sem_post (&thiz->s1)
printf ("pthread1: pthread1 unlock/n")
sleep (1)
}
return
}
static void* pthread_func_2 (PrivInfo* thiz)
{
return_if_fail (thiz != NULL)
while (time (NULL) < thiz->end_time)
{
sem_wait (&thiz->s1)
printf ("pthread2: pthread2 get the unlock./n")
sem_post (&thiz->s2)
printf ("pthread2: pthread2 unlock./n")
sleep (1)
}
return
}
姓名:冯成 学号:19020100164 学院:丁香二号书院转自:https://feixiaoxing.blog.csdn.net/article/details/7226657
【嵌牛导读】本文将介绍linux下的C语言开发中如何进行信号处理
【嵌牛鼻子】linux C语言 信号
【嵌牛提问】linux下的C语言开发中如何进行信号处理?
信号处理是linux程序的一个特色。用信号处理来模拟 *** 作系统的中断功能,对于我们这些系统程序员来说是最好的一个选择了。要想使用信号处理功能,你要做的就是填写一个信号处理函数即可。一旦进程有待处理的信号处理,那么进程就会立即进行处理。
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
int value = 0
void func(int sig)
{
printf("I get a signal!\n")
value = 1
}
int main()
{
signal(SIGINT, func)
while(0 == value)
sleep(1)
return 0
}
为了显示linux对signal的处理流程,我们需要进行两个步骤。第一,输入gcc sig.c -o sig, 然后输入./sig即可;第二则重启一个console窗口,输入ps -aux | grep sig, 在获取sig的pid之后然后输入kill -INT 2082, 我们即可得到如下的输出。
[root@localhost fork]#./sig
I get a signal!
[root@localhost fork]#
简单处理了一下,希望对你有帮助#define err_sys( str ) printf("error:%s\n" , str )
static void sig_int(int signo)
{
struct tms timebuf
int wallclock=times(&timebuf)
fprintf(stderr,"clock ticks since system startup are %d,\n",wallclock)
}
static void sig_term(int signo)
{
struct tms timebuf
int wallclock=times(&timebuf)
fprintf(stderr,"clock ticks since system start are %d,\n",wallclock)
exit(0)//终止程序
}
static void sig_alrm(int signo)
{
static int times=1
alarm(0)//输出时,不再计时
printf("time:%d\n" , times*10 )
times++
alarm(10)//重新开始计时
}
int main()
{
if (signal(SIGINT,sig_int)==SIG_ERR)
err_sys("can't catch SIGINT")
if (signal(SIGTERM,sig_term)==SIG_ERR)
err_sys("can't catch SIGTERM")
alarm(10)//开始计时
if (signal(SIGALRM,sig_alrm)==SIG_ERR)
err_sys("can't catch SIGALRM")
while(1) //等待在这里
return(0)
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)