给其他进程发信号,代码
时间: 2023-12-26 07:50:42 浏览: 17
以下是一个C语言程序,用于给其他进程发送信号:
```c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <pid> <signum>\n", argv[0]);
return 1;
}
int pid = atoi(argv[1]);
int signum = atoi(argv[2]);
if (kill(pid, signum) == -1) {
perror("kill");
return 1;
}
printf("Signal %d sent to process %d.\n", signum, pid);
return 0;
}
```
该程序需要传入两个参数,第一个参数是要发送信号的进程的PID,第二个参数是要发送的信号编号。例如,要向PID为1234的进程发送SIGTERM信号,可以运行以下命令:
```
./send-signal 1234 15
```
其中,15是SIGTERM的信号编号。
相关问题
信号量解决进程同步问题代码
下面是一个使用信号量来解决进程同步问题的示例代码,其中两个进程通过信号量来同步它们的操作,确保它们不会同时访问共享资源:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#define KEY 1234
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
};
// 初始化信号量
int init_sem(int sem_id, int init_val) {
union semun sem_union;
sem_union.val = init_val;
return semctl(sem_id, 0, SETVAL, sem_union);
}
// P 操作
int sem_p(int sem_id) {
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = -1;
sem_b.sem_flg = SEM_UNDO;
return semop(sem_id, &sem_b, 1);
}
// V 操作
int sem_v(int sem_id) {
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_op = 1;
sem_b.sem_flg = SEM_UNDO;
return semop(sem_id, &sem_b, 1);
}
// 删除信号量
int del_sem(int sem_id) {
union semun sem_union;
return semctl(sem_id, 0, IPC_RMID, sem_union);
}
int main() {
int sem_id, pid;
key_t key = KEY;
// 创建信号量
sem_id = semget(key, 1, IPC_CREAT | 0666);
if (sem_id == -1) {
perror("create semaphore error");
exit(EXIT_FAILURE);
}
// 初始化信号量的值为 1
if (init_sem(sem_id, 1) == -1) {
perror("init semaphore error");
exit(EXIT_FAILURE);
}
// 创建子进程
pid = fork();
if (pid == -1) {
perror("fork error");
exit(EXIT_FAILURE);
}
else if (pid == 0) {
// 子进程
printf("child process: waiting for semaphore...\n");
sem_p(sem_id);
printf("child process: accessing shared resource...\n");
sleep(5);
sem_v(sem_id);
printf("child process: releasing semaphore...\n");
exit(EXIT_SUCCESS);
}
else {
// 父进程
printf("parent process: waiting for semaphore...\n");
sem_p(sem_id);
printf("parent process: accessing shared resource...\n");
sleep(5);
sem_v(sem_id);
printf("parent process: releasing semaphore...\n");
wait(NULL);
}
// 删除信号量
if (del_sem(sem_id) == -1) {
perror("delete semaphore error");
exit(EXIT_FAILURE);
}
return 0;
}
```
在这个示例代码中,两个进程通过调用 `sem_p()` 和 `sem_v()` 函数来进行 P 操作和 V 操作,从而实现对共享资源的互斥访问和同步操作。其中,子进程等待信号量并进行 P 操作后,访问共享资源,然后进行 V 操作并释放信号量;父进程也等待信号量并进行 P 操作后,访问共享资源,然后进行 V 操作并释放信号量。
非父子进程间通信信号,代码
下面是一个使用信号进行非父子进程间通信的示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void handler(int sig) {
printf("Received signal %d\n", sig);
}
int main() {
pid_t pid1, pid2;
// 创建子进程1
pid1 = fork();
if (pid1 == 0) {
// 子进程1
printf("Child 1 (pid=%d) is running\n", getpid());
sleep(5);
// 向父进程发送信号
kill(getppid(), SIGUSR1);
exit(0);
} else {
// 创建子进程2
pid2 = fork();
if (pid2 == 0) {
// 子进程2
printf("Child 2 (pid=%d) is running\n", getpid());
sleep(10);
// 向父进程发送信号
kill(getppid(), SIGUSR2);
exit(0);
} else {
// 父进程
printf("Parent (pid=%d) is running\n", getpid());
// 注册信号处理函数
signal(SIGUSR1, handler);
signal(SIGUSR2, handler);
// 等待信号
int count = 0;
while (count < 2) {
sleep(1);
count++;
}
printf("Parent exiting\n");
exit(0);
}
}
return 0;
}
```
该程序创建了两个子进程,分别运行5秒和10秒,然后向父进程发送信号SIGUSR1和SIGUSR2。父进程注册了信号处理函数handler,等待接收两个信号后退出。注意,这里使用了两个不同的信号来区分不同的子进程发送的信号。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)