Skip to content

Instantly share code, notes, and snippets.

@rachejazz
Created July 24, 2020 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rachejazz/3228be9f0a74cbf1ab1c5674e110db6b to your computer and use it in GitHub Desktop.
Save rachejazz/3228be9f0a74cbf1ab1c5674e110db6b to your computer and use it in GitHub Desktop.
This is process 2: He wants to finish the fight with his neighbour once and for all, thus gives the user the choice of action.
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
/*the tree is made to demonstrate some time lapse after the user inputs the action.
After the user input the process will TAKE SOMETIME to send the signal through shared memory route*/
struct point {
double x, y;
};
struct vector {
int n;
struct point *p;
};
struct tree {
struct tree *left, *right;
int key;
struct vector *vector;
};
struct tree * maketree(int key)
{
struct tree * tree = (struct tree*)malloc(sizeof(struct tree));
tree->key=key;
tree->left=NULL;
tree->right=NULL;
return tree;
}
struct tree * find (struct tree *tree, int key)
{
if (! tree)
return 0;
if (key < tree->key)
return find (tree->left, key);
else if (key > tree->key)
return find (tree->right, key);
else if(key == tree->key)
return key;
//else return 0;
}
void main(int argc, char **argv[])
{
pid_t pid;
key_t keygen;
int shmid;
pid_t *shmptr;
int i,randt;
struct tree* randv=maketree(2);
randv->left=maketree(1);
randv->right=maketree(3);
printf("Neighbour is annoying. Kidnap him(1) or kill him(2) or let him go(3)?\n");
scanf("%d",&randt);
/*Access to the shared memory created by neighbour 1 is given here.
Remember: If this process exits before sending the signal, you need to kill neighbour 1 externaly*/
keygen= ftok(".",'s');
shmid=shmget(keygen, sizeof(pid_t), 0666);
shmptr=(pid_t*)shmat(shmid, NULL, 0);
pid=*shmptr;
shmdt(shmptr);
while(1){
i=find(randv, randt);
sleep(10);
if(i==0){
kill(pid, SIGUSR1);
printf("Sent SIGUSR1 signal to neighbour. Wrong input. Neighbour %d is avenged\n",pid);
exit(0);
}
else{
if(i==1){
kill(pid, SIGINT);
printf("Sent SIGINT signal to neighbour. Neighbour %d is kidnapped\n",pid);
exit(0);
}
if(i==2){
kill(pid, SIGQUIT);
printf("Sent SIGKILL signal to neighbour. Neighbour %d is killed\n", pid);
exit(0);
}
if(i==3){
kill(pid,SIGUSR2);
printf("Sent SIGUSR2 signal to neighbour. Neighbour %d is spared\n",pid);
exit(0);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment