You are on page 1of 11

Multi Core and Parallel Programming Assignment Answers

1.How threads are created and maintained in POSIX.


2. Explain four schedule schemes in OpenMP?
5.Write a program to create fibers?
//write a program to create fibers in parallel programming ?

#define _WIN32_WINNT 0x400

#include<stdio.h>
#include<windows.h>

#define FIBER_COUNT 10
void *fiber_context[FIBER_COUNT];

VOID WINAPI fiberProc(void *);

void main()
{
int i;
int fibers[FIBER_COUNT];

for (i=0;i<FIBER_COUNT; i++)


fibers[i] = i;

fiber_context[0] = ConvertThreadToFiber(NULL);

for(i=1;i<FIBER_COUNT;i++)
{
fiber_context[i] = CreateFiber(0,
fiberProc,
&fibers[i]);
if(fiber_context[i] != NULL)
printf("fiber %d created\n",i);
}
for(i=1;i<FIBER_COUNT;i++)
{
if(fiber_context[i] != NULL)
SwitchToFiber(fiber_context[i]);
}
}

VOID WINAPI fiberProc(void *fiber_nbr)


{
int nbr;

nbr = *((int*) fiber_nbr);


printf("Hello from fiber %d\n",nbr);
//now switch back to the fiber of the main line
SwitchToFiber(fiber_context[0]);
}
10. Write a program for creation of threads in .NET Framework.
//Write a program for creation of threads in .NET Framework.

using System;
using System.Threading;

public class ThreadDemo1


{
public static void ThreadFunc()
{
for( int i=0;i<3;i++)
Console.WriteLine(
"Hello #{0} from ThreadFunc", i);
Thread.Sleep(1000);
}
//The main entry point for the application.
public static void Main()
{
Thread t =
new Thread( new threadStart( ThreadFunc ));
t.start();
Thread.Sleep( 40 );

for( int j=0;j<4;j++)


{
Console.WriteLine( " Hello from Main Thread" );
Thread.Sleep( 0 );
}
}
}
7.Explain task queuing execution model.
8. Write the most commonly used OpenMP environment variables and library
Functions.
3. Explain how openMP achieves thread synchronization using barrier and
Nowait.?

You might also like