Skip to content

Instantly share code, notes, and snippets.

@kjmkznr
Created October 11, 2014 08:12
Show Gist options
  • Save kjmkznr/e7c7e3ccaf980876acfb to your computer and use it in GitHub Desktop.
Save kjmkznr/e7c7e3ccaf980876acfb to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include "libexample.h"
void helloworld() {
printf("Hello World\n");
}
void helloworld();
INSTALL = /usr/bin/install -c
prefix = /usr/local
libdir = $(prefix)/lib
default:
$(CC) -Wall -fPIC -c libexample.c
$(CC) -Wall -shared -Wl,-soname=libexample.so -o libexample.so libexample.o
clean:
@rm -f libexample.o libexample.so
install:
$(INSTALL) libexample.so $(libdir)
.PHONY: clean
/* $ gcc -I. -L. -lexample test-fork.c -o test-fork */
#include <libexample.h>
#include <stdio.h>
#include <stdio.h>
#include <unistd.h>
int main() {
int status;
printf("[Parent] PID is %d\n", getpid());
printf("[Parent] ");
helloworld();
while(1) {
pid_t pid = fork();
if (pid == 0) {
printf("[Child] ");
helloworld();
printf("[Child] PID is %d\n", getpid());
sleep(1);
} else if (pid < 0) {
_exit(-1);
} else {
waitpid(pid, &status, 0);
}
}
}
/* $ gcc -I. -L. -lexample test.c -o test */
#include <libexample.h>
#include <stdio.h>
#include <stdio.h>
int main() {
helloworld();
printf("PID is %d\n", getpid());
printf("Press Any Key To Exit...");
fgetc(stdin);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment