Operating Systems: CSCI03I04

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Operating Systems

CSCI03I04 Lab 3

Threads: Threads are often called lightweight processes. However they are not processes A Thread is a small set of executable instructions, which can be used to isolate a task from a process. Multiple threads are efficient way to obtain parallelism of hardware and give interactive user interaction to your applications. C# Thread: . Net Framework has thread-associated classes in System.Threading namespace. The following steps demonstrate how to create a thread in C#. Step 1: Create a System.Threading.Thread object. Step 2: Create the call back function. Step 3: Starting the Thread. Example 1:
using System; using System.Text; using System.Threading; namespace Test { class Program { static void Main() { Thread t = new Thread(WriteY); t.Start();

// create a new thread // start it

// Simultaneously, do something on the main thread. for (int i = 0; i < 1000; i++) Console.Write("x"); }

static void WriteY() { for (int i = 0; i < 1000; i++) Console.Write("y"); } } }

Example 2:
using System; using System.Text; using System.Threading; namespace Test { class Program { static void Main(string[] args) { Thread t1 = new Thread(FirstParallel); t1.Start(); Thread t2 = new Thread(SecParallel); t2.Start(); Console.WriteLine("Main thread Running"); } static void FirstParallel() { for (int i = 0; i < 10; i++) { Console.WriteLine("finished???"); Thread.Sleep(4000); } } static void SecParallel() { for (int i = 0; i < 10; i++) { Console.WriteLine("Noooooooooooooooooooooooooooooooooooooooo"); Thread.Sleep(2500); } } } }

File Operations in C#:

The FileStream Class: The FileStream class in the System.IO namespace helps in reading from, writing to and closing files. This class derives from the abstract class Stream. You need to create a FileStream object to create a new file or open an existing file. The syntax for creating a FileStream object is as follows:
FileStream F =new FileStream("sample.txt",FileMode.Open,FileAccess.Read,FileShare.Read);
Parameter Description The FileMode enumerator defines various methods for opening files. The members of the FileMode enumerator are:

FileMode

Append: It opens an existing file and puts cursor at the end of file, or creates the file, if the file does not exist. Create: It creates a new file. CreateNew: It specifies to the operating system, that it should create a new file. Open: It opens an existing file. OpenOrCreate: It specifies to the operating system that it should open a file if it exists, otherwise it should create a new file. Truncate: It opens an existing file and truncates its size to zero bytes.

FileAccess

FileAccess enumerators have members: Read, ReadWrite and Write. FileShare enumerators have the following members:

FileShare

Inheritable: It allows a file handle to pass inheritance to the child processes None: It declines sharing of the current file Read: It allows opening the file for reading ReadWrite: It allows opening the file for reading and writing Write: It allows opening the file for writing

So.. Writing in a file: Example:


using using using using System; System.Text; System.Threading; System.IO;

namespace Test { class Program { static void Main(string[] args) { FileStream fs = new FileStream("test.txt", FileMode.Append,FileAccess.Write); StreamWriter sw = new StreamWriter(fs); Console.WriteLine("Please write The content of the file:"); string str = Console.ReadLine(); sw.WriteLine(str); sw.Flush(); sw.Close(); fs.Close(); } } }

Reading from a file:


using using using using System; System.Text; System.Threading; System.IO;

namespace Test { class Program { static void Main(string[] args) { FileStream fs = new FileStream("test.txt", FileMode.Open,FileAccess.Read); StreamReader sr = new StreamReader(fs); Console.WriteLine("Here is the content of the file:"); string str = sr.ReadLine(); while (str != null) { Console.WriteLine(str); str = sr.ReadLine(); } Console.ReadLine(); sr.Close(); fs.Close(); } } }

You might also like