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

84 views
Skip to first unread message

woody stanford

unread,
Feb 26, 2017, 12:44:53 AM2/26/17
to BeagleBoard
Here is how you do the background process thing on a Beagle, even stock.


A claim.

woody stanford

unread,
Mar 3, 2017, 12:31:19 AM3/3/17
to BeagleBoard
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:

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

IMPORTANT: remember to "chmod 777 pinmux.sh" or it won't run. ie. make is executable.


[code]
#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);

}

[/code]

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

woody stanford

unread,
Mar 3, 2017, 1:08:33 AM3/3/17
to BeagleBoard
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).

[code]
#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);

}

[/code]

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

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

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

On Saturday, February 25, 2017 at 10:44:53 PM UTC-7, woody stanford wrote:

woody stanford

unread,
Mar 9, 2017, 12:58:40 PM3/9/17
to BeagleBoard
OK, in summary, what is the MAGIC piece of code here...the crux...the essence:

***********************************
***** BOOKMARK THIS ******
***********************************

[code]
      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);
[/code]

Get this code tattooed on your butt. Took me days to fully work it out and you have automation licked. a way to do it. :D


On Saturday, February 25, 2017 at 10:44:53 PM UTC-7, woody stanford wrote:
Reply all
Reply to author
Forward
0 new messages