How to Formally Invoke a "Background Process" in C on a BBW

Here is how you do the background process thing on a Beagle, even stock.

https://www.raspberrypi.org/forums/viewtopic.php?f=33&t=175567

A claim.

ok, here is a good example of how you can run a BBB-specific shell script with the DAEMON1 code examples. It reads back to you the current configurations for pins on the P8/9 headers. Its not just a cat of a config file but saves to a text file AND is entirely available in a C program.

You can also read the resulting text file in python btw.

IMPORTANT: requires the shell script to be in the same directory as the executable. Make “pinmux.sh” and put these lines of shell script in it:


cat /sys/kernel/debug/pinctrl/44e10800.pinmux/pingroups > ./pinmux.txt
sleep 1
exit

IMPORTANT: remember to “chmod 777 pinmux.sh” or it won’t run. ie. make is executable.

I got the info for it from: http://derekmolloy.ie/gpios-on-the-beaglebone-black-using-device-tree-overlays/


#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>

main()
{
int ppid;
int status;
FILE *fptr;
char c;

remove("pinmux.txt");

printf("generating pin group data /sys/kernel/debug/pinctrl/44e10800.pinmux/pingroups...\n");
fflush(NULL);

if ((ppid=fork())==0)
{
printf("Child process\n");
execlp("bash","bash","./pinmux.sh","",NULL);
return; //terminates child process
}

//does this block? YES!
// printf("Parent process %d\n",ppid);
wait(&status);

fptr=fopen("pinmux.txt","r");

while(!feof(fptr))
{
fread(&c,1,1,fptr);
printf("%c",c);
}

fclose(fptr);

}

Compile with “gcc pinmux.c -o pinmux -lm” and execute with “./pinmux”

Here is another powerful example of how a correctly set up background process can fetch a web page for you.

Using URL_encode() you can even invoke specific scripts on a remote host via HTTP and HTTPS protocol using CURL (available on stock Angstrom but remember to connect it to the inet).


#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>

main()
{
int ppid;
int status;
FILE *fptr;
char c;

remove("woody.txt");

printf("Reading the HTML source from Stanford Systems...\n");
fflush(NULL);

if ((ppid=fork())==0)
{
printf("Child process\n");
execlp("bash","bash","./ecurl.sh","",NULL);
return; //terminates child process
}

//does this block? YES!
// printf("Parent process %d\n",ppid);
wait(&status);

fptr=fopen("woody.txt","r");

printf("\n\nSource code of Woody's home page.\n\n");

while(!feof(fptr))
{
fread(&c,1,1,fptr);
printf("%c",c);
}

fclose(fptr);

}

Again requires a short shell script to do the actual operation. In this case, put the following shell scripting into “ecurl.sh”:


curl -k http://wodystanford.wordpress.com > ./woody.txt
sleep 1
exit

make executable, and compile C portion and execute as in the previous example.

OK, in summary, what is the MAGIC piece of code here…the crux…the essence: