In: Computer Science
You will create a number of threads—for example, 100—and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period of time approximates the typical pid usage in which a pid is assigned to a new process, the process executes and then terminates, and the pid is released on the process's termination.) On UNIX and Linux systems, sleeping is accomplished through the sleep() function, which is passed an integer value representing the number of seconds to sleep.
Write the program in C .
Code:
Java thread examole by extending thread class
class Multi extends Thread
{
public void run()
{
System.out.println("Thread is running....!");
}
public static void main(String args[])
{
Multi t1=new Multi();
t1.start();
}
}
//Thread example by implementing runnable interfaces
class Multi2 implements Runnable
{
public void run()
{
System.out.println("Thread is running...---");
}
public static void main(String args[])
{
Multi2 m1=new Multi2();
Thread t1=new Thread(m1);
t1.start();
}
}
// Sleep method example
class SleepMethod extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
try{Thread.sleep(500);}catch(InterruptedException e)
{System.out.println(e);}
System.out.print(i);
}
}
public static void main(String args[])
{
SleepMethod t1=new SleepMethod();
SleepMethod t2=new SleepMethod();
t1.start();
t2.start();
}
}
//Join method example
class Multi extends Thread
{
public void run()
{
System.out.println("Running...");
System.out.println(Thread.currentThread().getName());
}
public static void main(String args[])
{
Multi t1=new Multi();
Multi t2=new Multi();
System.out.println("Name of t1: "+t1.getName());
System.out.println("Name of t2: "+t2.getName());
System.out.println("Id of t1: "+t1.getId());
System.out.println("Id of t2: "+t2.getId());
System.out.println("Priority of t1: "+t1.getPriority());
t1.setName("Saikumar");
t1.start();
System.out.println("Name of t1 after changing:
"+t1.getName());
}
}
Output: