Last active
April 14, 2018 16:51
-
-
Save Miraj50/bc52c288952da338e21b8e0959494245 to your computer and use it in GitHub Desktop.
Control one C program from another C program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <bits/stdc++.h> | |
#include <unistd.h> | |
int main(){ | |
for(int i=0;;i++){ | |
printf("Ex.c : %d\n",i); | |
sleep(1); | |
} | |
} | |
// Prints integers in intervals of 1 second |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <bits/stdc++.h> | |
#include <unistd.h> | |
int main(){ | |
for(int i=0;;i++){ | |
printf("Ex2.c : %d\n",i); | |
sleep(1); | |
if(i == 3){ | |
return 5; // return status | |
} | |
} | |
} | |
// Prints integers in intervals of 1 second |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
g++ ex1.c -o simp | |
g++ ex.c | |
./a.out & # Run the two C programs simultaneously | |
./simp | |
A=$? # Store return status of ex1.c | |
PID=$! # Store Process ID of the program going on | |
if [ $A -eq 5 ]; then | |
kill $PID # Kill the continuing program | |
fi | |
# Run ./Master_script.sh | |
# What the program does is that if ex1.c terminates, it terminates ex.c as well. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment