
正文
linux命令销毁信号量 linux删除命令警告
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
linux进程间信号量的分配释放
和用于分配、释放共享内存的 shmget 和 shmctl 类似,系统调用 semget 和 semctl 负责分配、释放信号量。调用 semget 函数并传递如下参数:一个用于标识信号量组的键值,该组中包含的信号量数量和与 shmget 所需的相同的权限位标识。该函数返回的是信号量组的标识符。您可以通过指定正确的键值来获取一个已经存在的信号量的标识符;这种情况下,传递的信号量组的容量可以为0。
信号量会一直保存在系统中,甚至所有使用它们的进程都退出后也不会自动被销毁。最后一个使用信号量的进程必须明确地删除所使用的信号量组,来确保系统中不会有太多闲置的信号量组,从而导致无法创建新的信号量组。可以通过调用semctl来删除信号量组。调用时的四个参数分别为信号量组的标识符,操作的信号量在组中的编号、常量IPC_RMID 和一个 union semun 类型的任意值(被忽略)。调用进程的有效用户 id 必须与分配这个信号量组的用户 id 相同(或者调用进程为 root 权限亦可)。与共享内存不同,删除一个信号量组会导致 Linux 立即释放资源。
代码 5.2 展示了用于分配和释放一个二元信号量的函数。
代码 5.2 (sem_all_deall.c)分配和释放二元信号量
#include sys/ipc.h
#include sys/sem.h
#include sys/types.h /* 我们必须自己定义 semun 联合类型。 */
union semun { int val; struct semid_ds *buf; unsigned short int *array; struct seminfo *__buf; };
/* 获取一个二元信号量的标识符。如果需要则创建这个信号量 */
int binary_semaphore_allocation (key_t key, int sem_flags)
{
return semget (key, 1, sem_flags);
} /* 释放二元信号量。所有用户必须已经结束使用这个信号量。如果失败,返回 -1 */
int binary_semaphore_deallocate (int semid)
{
union semun ignored_argument; return semctl (semid, 1, IPC_RMID, ignored_argument);
}
相关问答
Q1: Linux kill 命令怎么用?
Linux kill 命令用于终止进程,其用法步骤如下:
需要准备的材料分别是:电脑、linux连接工具。
1、首先连接上linux主机,进入命令行状态。
2、输入:ps -ef,按回车,查询进程列表。
3、选择需要终止的进程,例如PID为9977的进程,则命令行输入:kill 9977,按回车即可终止该进程。
Q2: c语言实例,linux线程同步的信号量方式 谢谢
这么高的悬赏,实例放后面。信号量(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;
}
Q3: linux 的 kill -3
9是信号量,kill实际上是用来发送信号量给进程,你可以通过man kill查看信号量列表,这里给你一个我获得的信号量列表:
Name Num Action Description
0 0 n/a exit code indicates if a signal may be sent
ALRM 14 exit
HUP 1 exit
INT 2 exit
KILL 9 exit cannot be blocked
PIPE 13 exit
POLL exit
PROF exit
TERM 15 exit
USR1 exit
USR2 exit
VTALRM exit
STKFLT exit might not be implemented
PWR ignore might exit on some systems
WINCH ignore
CHLD ignore
URG ignore
TSTP stop might interact with the shell
TTIN stop might interact with the shell
TTOU stop might interact with the shell
STOP stop cannot be blocked
CONT restart continue if stopped, otherwise ignore
ABRT 6 core
FPE 8 core
ILL 4 core
QUIT 3 core
SEGV 11 core
TRAP 5 core
SYS core might not be implemented
EMT core might not be implemented
BUS core core dump might fail
XCPU core core dump might fail
XFSZ core core dump might fail
关于linux命令销毁信号量和linux删除命令警告的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







