Skip to content

Commit

Permalink
Initial Commit of Source Code.
Browse files Browse the repository at this point in the history
  • Loading branch information
greggagne committed Jul 3, 2014
0 parents commit bd180db
Show file tree
Hide file tree
Showing 36 changed files with 3,248 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_STORE
13 changes: 13 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
This contains source code for the 9th edition of
"Operating System Concepts" by Silberschatz/Galvin/Gagne.
Published by John Wiley & Sons.

Source files are organized by Chapter.

To download all files, enter

git clone https://github.com/greggagne/OSC9e.git

which will clone this repository on your local machine.

-- Greg Gagne July 2014.
77 changes: 77 additions & 0 deletions ch11/LockingExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* A simple program demonstrating file locking.
* This program acquires an exclusive lock on the
* first half of the file and a shared lock on the second half.
*
* Usage
* java LockingExample <input file>
*
* Figure 11.2
*
* @author Gagne, Galvin, Silberschatz
* Operating System Concepts - Ninth Edition
* Copyright John Wiley & Sons - 2013.
*/

import java.io.*;
import java.nio.channels.*;

public class LockingExample {
public static final boolean EXCLUSIVE = false;
public static final boolean SHARED = true;

public static void main(String args[]) throws IOException {
if (args.length != 1) {
System.err.println("Usage: java LockingExample <input file>");
System.exit(0);
}

FileLock sharedLock = null;
FileLock exclusiveLock = null;

try {
RandomAccessFile raf = new RandomAccessFile(args[0], "rw");

// get the channel for the file
FileChannel channel = raf.getChannel();

System.out.println("trying to acquire lock ...");
// this locks the first half of the file - exclusive
exclusiveLock = channel.lock(0, raf.length()/2, EXCLUSIVE);
System.out.println("lock acquired ...");

/**
* Now modify the data . . .
*/

try {
// sleep for 10 seconds
Thread.sleep(10000);
}
catch (InterruptedException ie) { }

// release the lock
exclusiveLock.release();
System.out.println("lock released ...");

// this locks the second half of the file - shared
sharedLock = channel.lock(raf.length()/2 + 1, raf.length(), SHARED);

/**
* Now read the data . . .
*/

// release the lock
exclusiveLock.release();
} catch (java.io.IOException ioe) {
System.err.println(ioe);
}
finally {
if (exclusiveLock != null)
exclusiveLock.release();
if (sharedLock != null)
sharedLock.release();
}
}
}

16 changes: 16 additions & 0 deletions ch11/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
This contains the example Java program showing how file locking is
provided. This example has been modified somewhat from the text in that
the file being locked is passed on the command line.

Also, the thread produces an output message before trying to acquire
the lock and after it has released it. Furthermore, once it has acquired
the lock, thread waits ten seconds before releasing it.

On systems supporting either manadatory (i.e. Windows) or advisory
(i.e. UNIX) locking, running two copies of this program illustrates how
the Java file locking API works.

Another test is to first run the Java program to acquire
the file lock. Next, try opening the locked file using a standard text editor
or word processor. Compare the different behavior on mandatory and advisory
systems.
1 change: 1 addition & 0 deletions ch11/lockfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is the file we will acquire the lock on.
1 change: 1 addition & 0 deletions ch12/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is the first example file.
1 change: 1 addition & 0 deletions ch12/file3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is the file for soft links.
36 changes: 36 additions & 0 deletions ch17/DNSLookUp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* A simple program demonstrating DNS lookup
*
* Usage:
* java DNSLookUp <IP Name>
*
* Figure 16.5
* @author Gagne, Galvin, Silberschatz
* Operating System Concepts - Ninth Edition
* Copyright John Wiley & Sons - 2013.
*
*/

import java.io.*;
import java.net.*;

public class DNSLookUp
{
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java DNSLookUp <IP name>");
System.exit(0);
}

InetAddress hostAddress;

try {
hostAddress = InetAddress.getByName(args[0]);
System.out.println(hostAddress.getHostAddress());
}
catch (UnknownHostException uhe) {
System.err.println("Unknown host: " + args[0]);
}
}
}
5 changes: 5 additions & 0 deletions ch2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
obj-m += simple.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
25 changes: 25 additions & 0 deletions ch2/simple.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

/* This function is called when the module is loaded. */
int simple_init(void)
{
printk(KERN_INFO "Loading Module\n");

return 0;
}

/* This function is called when the module is removed. */
void simple_exit(void) {
printk(KERN_INFO "Removing Module\n");
}

/* Macros for registering module entry and exit points. */
module_init( simple_init );
module_exit( simple_exit );

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Simple Module");
MODULE_AUTHOR("SGG");

32 changes: 32 additions & 0 deletions ch3/DateClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Client program requesting current date from server.
*
* Figure 3.22
*
* @author Silberschatz, Gagne and Galvin
* Operating System Concepts - Eighth Edition
*/

import java.net.*;
import java.io.*;

public class DateClient
{
public static void main(String[] args) {
try {
// this could be changed to an IP name or address other than the localhost
Socket sock = new Socket("127.0.0.1",6013);
InputStream in = sock.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));

String line;
while( (line = bin.readLine()) != null)
System.out.println(line);

sock.close();
}
catch (IOException ioe) {
System.err.println(ioe);
}
}
}
38 changes: 38 additions & 0 deletions ch3/DateServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

/**
* Time-of-day server listening to port 6013.
*
* Figure 3.21
*
* @author Silberschatz, Gagne, and Galvin.
* Operating System Concepts - Ninth Edition
* Copyright John Wiley & Sons - 2013.
*/

import java.net.*;
import java.io.*;

public class DateServer
{
public static void main(String[] args) {
try {
ServerSocket sock = new ServerSocket(6013);

// now listen for connections
while (true) {
Socket client = sock.accept();
// we have a connection

PrintWriter pout = new PrintWriter(client.getOutputStream(), true);
// write the Date to the socket
pout.println(new java.util.Date().toString());

// close the socket and resume listening for more connections
client.close();
}
}
catch (IOException ioe) {
System.err.println(ioe);
}
}
}
29 changes: 29 additions & 0 deletions ch3/fork-question-1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Solution to question 3.1
*
* Answer is 5 as the child and parent processes
* each have their own copy of value.
*/

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int value = 5;

int main()
{
pid_t pid;

pid = fork();

if (pid == 0) { /* child process */
value += 15;
return 0;
}
else if (pid > 0) { /* parent process */
wait(NULL);
printf ("PARENT: value = %d\n",value); /* LINE A */
return 0;
}
}
25 changes: 25 additions & 0 deletions ch3/fork-question-2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Solution to exercise 3.2
*
* The answer is easily determined by counting
* the number of unique processes which are output
* by the call to getpid() - which is 8 unique processes.
*/

#include <stdio.h>

int main()
{
printf("%d\n",getpid());
fork();
printf("%d\n",getpid());


fork();
printf("%d\n",getpid());

fork();
printf("%d\n",getpid());

return 0;
}
39 changes: 39 additions & 0 deletions ch3/newproc-posix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* This program forks a separate process using the fork()/exec() system calls.
*
* Figure 3.09
*
* @author Silberschatz, Galvin, and Gagne
* Operating System Concepts - Ninth Edition
* Copyright John Wiley & Sons - 2013
*/

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
pid_t pid;

/* fork a child process */
pid = fork();

if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed\n");
return 1;
}
else if (pid == 0) { /* child process */
printf("I am the child %d\n",pid);
execlp("/bin/ls","ls",NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
printf("I am the parent %d\n",pid);
wait(NULL);

printf("Child Complete\n");
}

return 0;
}
Loading

0 comments on commit bd180db

Please sign in to comment.