NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Directory entries pointing to file with link -Two directory entries refer to the same file but with different names
•Hard link is the directory entry of a file or another directory pointing to inode directly
•Any changes to inode of original file does not affect the hard link
•Cannot span across file systems
•Symbolic (Soft) link is the directory entry of a file or another directory pointing to another directory entry which will either main entry or will have hard or soft link to main entry
•Changes to inodein original file will affect the symbolic link or soft link
•Can span across file system

File Types:
S_IFREG –regular file
S_IFDIR –directory
S_IFLINK –symbolic link

S_ISREG(mode) –check if mode value indicates regular file...
S_ISDIR(mode)
S_ISLNK(mode)

S_IRUSR-read permission, owner
S_IWUSR -write permission, owner
S_IXUSR -execute/search permission, owner
S_IRWXU -read, write, execute/search by owner

Similarly GRP/G and OTH/O

struct stat st, lst;
// Check file types using macros
if (S_ISLNK(lst.st_mode))
printf("%s is symbolic linkn", argv[1]);
else if (S_ISREG(st.st_mode))
printf("%s is regular filen", argv[1]);
else if (S_ISDIR(st.st_mode))
printf("%s is a directoryn", argv[1]);

// Check for access
if (st.st_mode & S_IRUSR)
printf("Owner has read access to %s n", argv[1]);
//////////////////////////
dirent.h–format of directory entries
ino_t d_ino file inode(aka serial) number
char d_name[] name of entry
////////////////////////////
UnbufferedI/O: each read write invokes a system call in the kernel.
Buffered I/O: data is read/written in optimal-sized chunks from/to disk --> streams
////////////////////////
Open a File
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
flags:
O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_APPEND, O_TRUNC
mode:
0600 = 0 110 000 000 = - rw- --- ---
////////////////////////
int dup(int oldfd); → creates a copy of the file descriptor oldfd, using the lowest-numbered unused file descriptor for the new descriptor
////////////////////////
Read From a File = ssize_t read(int fd, void *buf, size_t count); where, count : number of bytes to read
-1 means error
>= 0 : number of bytes that were actually read from the file.

Difference between open and dup✅✅✅
////////////////////////
Write Into a File = ssize_t write(int fd, const void *buf, size_t count);
Close a File = int close(int fd);
////////////////////////
Buffering = to set the buffer
void setbuf(FILE *fp, char *buf);
void setvbuf(FILE *fp, char *buf, int mode, size_t size);

Opening a Stream = FILE *fopen(const char *pathname, const char *type);
////////////////////////
buffering ma fopen aavse...
fopen() flags -- open() flags
r -- O_RDONLY
w -- O_WRONLY | O_CREAT | O_TRUNC
a -- O_WRONLY | O_CREAT | O_APPEND
r+ -- O_RDWR
w+ -- O_RDWR | O_CREAT | O_TRUNC
a+ -- O_RDWR | O_CREAT | O_APPEND

Closing a Stream = int fclose(FILE *fp);
///////////////////////
Reading and Writing from/to Streams
Character-at-a-time getc()
int getc(FILE *fp);
Line-at-a-time fgets()
int fgetc(FILE * fp);
Direct fread()
Writing a char
int putc(int c, FILE *fp);
int fputc(int c, FILE *fp);
int putchar(int c);

Measure time spent in user mode and kernel mode using : time <program>

Copy file File_IOIOefficiency.txt of size 2,100,000 bytes (@2MB) to File_IOIOefficiency1.txt by redirecting stdin and stdout: File_IOIOefficiency.c
time ./IOefficiency.out <IOefficiency.txt >IOefficiency1.txt



✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅
Process Management

Process Relation Between Parent and Child Processes
• Resource sharing possibilities
• Parent and children share all resources
• Children share subset of parent ’s resources
• Parent and child share no resources
• Execution possibilities
• Parent and children execute concurrently
• Parent waits until children terminate
• Memory address space possibilities
• Address space of child duplicate of parent
• Child has a new program loaded into it
///////////
pid_t getpid(void)
pid_t getppid(void)
pid_t fork(void) => Returns 0 in child, Returns process ID of child in parent, -1 on error
pid_t vfork(void)
void exit(int)
pid-t wait(int *statloc)
pid_t wait(pid_t pid, int *statloc..)

Process States (6 State Model)
• Idle → process is being create by fork() system call and is not yet runnable
• Runnable → process is waiting for CPU to start running
• Running → process is current running executing instructions
• Sleeping → process is waiting for an event to occur
• Suspended → process has been “frozen” by signal such as SIGSTOP
• Zombified → process has terminated but has not yet returned its exit code to its parent.
////////////////
childPid = wait( &status ); // add wait in parent process to wait for child process, child will not become orphan
Zombie Process => parent was not ready to take(busy) the exit code of child process
////////////////
printf("Im process %d and Im about to exec an ls -l n", getpid() );
execl( "/bin/ls", "", "-l", NULL ); // Execute ls
printf("This line should never be executed n"); //bcoz of ls in upper line

✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅
Pipes
$ who | wc -l
The who utility generates one line of output per user. | pipe . “-l” with wc will count total output lines.

int pipe( int fd[2] ) (unnamed pipes) (Unidirectional)
fd[0] read
fd[1] write
//////////////////
READ and WRITE conditions for PIPE for OS Implementation:
*READ Conditions:*
• If a process reads from a pipe whose “write” end has been closed, the “read()” call returns a value of zero, indicating the end of input.
• If a process reads from an empty pipe whose “write” end is still open, it sleeps until some input becomes available.
• If a process tries to read more bytes from a pipe that are present, all of the current contents are returned and read() returns the number of bytes actually read.

*WRITE Conditions:*
• if a process writes to a pipe whose “read” end has been closed, the write fails and the writer process is sent a SIGPIPE signal. the default action of this signal is to terminate the process.
• If a process writes fewer bytes to a pipe than the pipe can hold, the write() is guaranteed to be atomic; that is, the writer process will complete its system call without being preempted by another process.
• If the kernel cannot allocate enough space for a new pipe, pipe() returns a value of -1; otherwise, it returns a value of 0.

Variable to be used in read and write algos.
READ_END=0 // pipefd read index i.e. pipefd[READ_END]
WRITE_END=1 // pipefd write index i.e. pipefd[WRITE_END]
read_offset=0 // offset for the next read from pipe
write_offset=0 // offset for the next write to pipe
pipebufsize=64*1024 // maximum size of pipebuf
String pipebuf // pipe wrap-around buffer to store pipe data
pipefilled // number (count) of bytes already in pipe <------

************************************
#define READ 0 // The index of the “read” end of the pipe
#define WRITE 1 // The index of the “write” end of the pipe
char* phrase ="Dummy data written to pipe";
int main(void)
{
int fd[2], bytesRead, status;
char message[100]; /* Parent process' message buffer */
pipe(fd); /* Create an unnamed pipe */
if ( fork() == 0 ) /* Child, write */
{
close(fd[READ]); /* Close unused end */
printf("Child: Sending message '%s' to parentn",phrase);
write(fd[WRITE], phrase, strlen(phrase)+1); /* Send */
close(fd[WRITE]); /* Close used end */
}
else /* Parent, reader */
{
close(fd[WRITE]); /* Close unused end */
bytesRead = read( fd[READ], message, 100 ); /* Receive */
printf("Parent: Read %d bytes: %s n", bytesRead, message );
wait(&status);
close(fd[READ]); /* Close used end */
}
}
*********************************************

Pipe are 1-way communication between parent and child that means for bidirectional (2-way) communication you need 2 pipes
int fd1[2], fd2[2];
pipe(fd1); pipe(fd2);

✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅✅
Signal Types:
• Some signals are asynchronous, they may be raised at any time (user pressing Ctrl+C)
• Some signals are directly related to hardware/process code (illegal instruction, arithmetic exception, such as attempt to divide by 0) - synchronous signals

Process suspended using Ctrl+Z (SIGTSTP) can be brought back to lifeusing fg command which sends (SIGCONT) signal to resume

SIGALRM using alarm() System Call
- If an alarm had already been scheduled, than an alarm is overwritten.
- The default handler for this signal displays the message “Alarm clock” and terminates the process.

***************************************
//alaram override
int alarmFlag = 0; //Global alarm flag
void alarmHandler(); // Forward declaration of alarm handler
void (*oldhandler)();
int main(void)
{
alarm(3); //Schedule an alarm signal in three seconds

oldhandler = signal( SIGALRM, alarmHandler ); // Install signal handler

printf("Looping n");
while( !alarmFlag ) // Loop until flag set
{
pause(); // Wait for a signal
}
printf("Loop ends due to alarm signal n");
}

void alarmHandler()
{
printf("An alarm clock signal was received n");
//alarmFlag=1;

// Set Default Handler for SIGALRM
signal(SIGALRM, oldhandler);
// Set another alam so default handler is executed
alarm(3);
}
***************************************

Ignore Control-C
void (*oldHandler)()= signal(SIGINT, SIG_IGN);
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.