<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="et">
	<id>http://www.lambda.ee/w/index.php?action=history&amp;feed=atom&amp;title=Loengun%C3%A4ited_paralleelv%C3%A4rkidest_2026</id>
	<title>Loengunäited paralleelvärkidest 2026 - Redigeerimiste ajalugu</title>
	<link rel="self" type="application/atom+xml" href="http://www.lambda.ee/w/index.php?action=history&amp;feed=atom&amp;title=Loengun%C3%A4ited_paralleelv%C3%A4rkidest_2026"/>
	<link rel="alternate" type="text/html" href="http://www.lambda.ee/w/index.php?title=Loengun%C3%A4ited_paralleelv%C3%A4rkidest_2026&amp;action=history"/>
	<updated>2026-11-07T00:47:39Z</updated>
	<subtitle>Selle lehekülje redigeerimiste ajalugu</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>http://www.lambda.ee/w/index.php?title=Loengun%C3%A4ited_paralleelv%C3%A4rkidest_2026&amp;diff=22833&amp;oldid=prev</id>
		<title>Tammet: Uus lehekülg: &#039;&lt;pre&gt;  #include &lt;stdio.h&gt; #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt;  #define MAX_COUNT 200  void ChildProcess(void); /* child process prototype */ void ParentProcess(void); /* p...&#039;</title>
		<link rel="alternate" type="text/html" href="http://www.lambda.ee/w/index.php?title=Loengun%C3%A4ited_paralleelv%C3%A4rkidest_2026&amp;diff=22833&amp;oldid=prev"/>
		<updated>2026-03-30T07:05:24Z</updated>

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