Loengunäited paralleelvärkidest 2026
Allikas: Lambda
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX_COUNT 200
void ChildProcess(void); /* child process prototype */
void ParentProcess(void); /* parent process prototype */
void main(void) {
pid_t pid;
pid = fork();
if (pid == 0) ChildProcess();
else ParentProcess();
}
void ChildProcess(void) {
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf(" This line is from child, value = %d\n", i);
printf(" *** Child process is done ***\n");
}
void ParentProcess(void){
int i;
for (i = 1; i <= MAX_COUNT; i++)
printf("This line is from parent, value = %d\n", i);
printf("*** Parent is done ***\n");
}
------
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
void alarmHandler(int sig) {
printf("\nalarm called\n");
exit(0);
}
void childHandler(int sig) {
printf("child called\n");
signal(SIGUSR1, childHandler);
}
int main() {
pid_t val;
if ((val = fork())) {
printf("parent cp1\n");
signal(SIGALRM, alarmHandler);
printf("parent cp2\n");
alarm(5); // in five seconds alarm will sound
printf("parent cp3\n");
while(1) {
sleep(1); // wait 1 sec
kill(val, SIGUSR1); // send signal to child
printf("parent cp4\n");
printf("parent cp5\n");
}
} else {
printf("child cp1\n");
signal(SIGUSR1, childHandler);
printf("child cp2\n");
while (1) {
sleep(1);
printf("child cp3\n");
}
}
printf("main to return\n");
return 0;
}
----
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message_function( void *ptr );
void main() {
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
}
void *print_message_function( void *ptr ) {
char *message;
message = (char *) ptr;
printf("%s \n", message);
}
--
#!/usr/bin/env python3
#Import os Library
import os
# Fork a child process
processid = os.fork()
print(processid)
# processid > 0 represents the parent process
if processid > 0 :
print("\nParent Process:")
print("Process ID:", os.getpid())
print("Child's process ID:", processid)
# processid = 0 represents the created child process
else :
print("\nChild Process:")
print("Process ID:", os.getpid())
print("Parent's process ID:", os.getppid())
---
#!/usr/bin/env python3
# Source - https://stackoverflow.com/a/16207592
# Posted by Mark Tolonen
# Retrieved 2026-03-30, License - CC BY-SA 3.0
import threading
from queue import Queue
import time
# lock to serialize console output
lock = threading.Lock()
def do_work(item):
time.sleep(.1) # pretend to do some lengthy work.
# Make sure the whole print completes or threads can mix up output in one line.
with lock:
print(threading.current_thread().name,item)
# The worker thread pulls an item from the queue and processes it
def worker():
while True:
item = q.get()
do_work(item)
q.task_done()
# Create the queue and thread pool.
q = Queue()
for i in range(4):
t = threading.Thread(target=worker)
t.daemon = True # thread dies when main thread (only non-daemon thread) exits.
t.start()
# stuff work items on the queue (in this case, just a number).
start = time.perf_counter()
for item in range(20):
q.put(item)
q.join() # block until all tasks are done
# "Work" took .1 seconds per task.
# 20 tasks serially would be 2 seconds.
# With 4 threads should be about .5 seconds (contrived because non-CPU intensive "work")
print('time:',time.perf_counter() - start)