Skip to content

Instantly share code, notes, and snippets.

@Miraj50
Last active April 14, 2018 16:51
Show Gist options
  • Save Miraj50/bc52c288952da338e21b8e0959494245 to your computer and use it in GitHub Desktop.
Save Miraj50/bc52c288952da338e21b8e0959494245 to your computer and use it in GitHub Desktop.
Control one C program from another C program
#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
#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
#!/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