Lab 2
Lab 2
Lab 2
Task (1)
(1.1) Copy the following c program using vi editor and save it as helloWorld.c.
(1.2) Compile and execute the above helloWorld.c (Include in Lab Report: how to compile and the
output)
=i) Press Esc button and then type :wq. It will save the file.
ii) To Compile the program:
Task 2
(2.1) Create a file named in1 using vi editor. Write the content of the file with the following text:
TMN 3053
System Programming
(2.2) Create a c program that will opens the file "in" in the current directory, and prints out the
return value of the open() system call. (Include the source code of this program, how to compile,
and the output).
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
int fd = open("in1", O_RDONLY);
printf("%d\n", fd);
return 0;
}
(2.3) What is the expected outcome for the following program? (include in Lab report)
(2.4) Write a program that will open the file "out2" for writing in the current directory. It uses
O_CREAT to create the file if it does not exist already, and O_TRUNC to truncate the file to zero
bytes if it does exist. Also include 345 permission for the mode_t mode. (Include the source code
of this program, how to compile, and the output).
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
int fd = open("out2", O_WRONLY | O_CREAT | O_TRUNC, 0x345);
printf("%d\n", fd);
return 0;
}
(2.5) Compile and execute close_example.c program available from eLEAP. Explain the output
(include in Lab report).
This output shows that the file "in1" was opened twice, resulting in two different file descriptors
being assigned (3 and 4 in this case). The program then closed both file descriptors and reopened
the file, resulting in a new file descriptor (3 in this case) being assigned to "fd2". Finally, the
program closed the file descriptor for "fd2" again.
TASK 3
(3.1) Write a c program that will read 10 bytes of the text file named in.txt (download from eLEAP)
and print to the standard output (stdout). Compile and execute the program. (Include the source
code of this program, how to compile, and the output).
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
int fd, num_bytes;
char buffer[11];
fd = open("in.txt", O_RDONLY);
if (fd < 0) {
perror("in.txt");
exit(1);
}
buffer[num_bytes] = '\0';
printf("%s", buffer);
if (close(fd) < 0) {
perror("close");
exit(1);
}
return 0;
}
Output:
Edit the above program, extend the program to read 99 bytes from the in.txt. Compile and execute
the program. (Include the source code of this program, how to compile, and the output).
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
int fd, num_bytes;
char buffer[99];
fd = open("in.txt", O_RDONLY);
if (fd < 0) {
perror("in.txt");
exit(1);
}
buffer[num_bytes] = '\0';
printf("%s", buffer);
if (close(fd) < 0) {
perror("close");
exit(1);
}
return 0;
}
Output:
(3.2) Write a program that will open the file out3.txt in the current directory for writing (include
oflag O_CREAT and O_TRUNC), and writes the string "TME4133\n" to it. Printout the return value
of the write() system call. (Include the source code of this program, how to compile, and the
output).
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd, num_bytes;
char buffer[] = "TME4133\n";
if (close(fd) < 0) {
perror("close");
exit(1);
}
return 0;
}
Output:
(3.3) Write a very simple_cat.c program that will read the input from a file input and write to the
terminal. For example, the output of the program is following: (Include the source code of this
program, how to compile, and the output).
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd, num_bytes;
char buffer[1024];
fd = open("in1", O_RDONLY);
if (fd < 0) {
perror("in1");
return 1;
}
if (num_bytes < 0) {
perror("read");
return 1;
}
if (close(fd) < 0) {
perror("close");
return 1;
}
return 0;
}
Output:
TASK 4
(4.1) Compile and execute lseek_example.c program available from eLEAP. (Include the source
code of this program, how to compile, and the output). Explain the output (include in Lab report).
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Int main()
{
char *c;
int fd, sz, i;
c = (char *) calloc(100, sizeof(char));
fd = open("in1", O_RDONLY);
if (fd < 0) { perror("r1"); exit(1); }
sz = read(fd, c, 10);
printf("We have opened in1, and called read(%d, c, 10).\n", fd);
printf("It returned that %d bytes were read.\n", sz);
c[sz] = '\0';
printf("Those bytes are as follows: %s\n", c);
i = lseek(fd, 0, SEEK_CUR);
printf("lseek(%d, 0, SEEK_CUR) returns that the current offset of the file is %d\n\n", fd, i);
printf("now, we seek to the beginning of the file and call read(%d, c, 10)\n",
fd);
lseek(fd, 0, SEEK_SET);
sz = read(fd, c, 10);
c[sz] = '\0';
printf("The read returns the following bytes: %s\n", c);
printf("now, we do lseek(%d, -6, SEEK_END). It returns %lld\n", fd, (long long)lseek(fd, -6, SEEK_END));
printf("If we do read(%d, c, 10), we get the following bytes: ", fd);
sz = read(fd, c, 10);
c[sz] = '\0';
printf("%s\n", c);
printf("Finally, we do lseek(%d, -1, SEEK_SET). This returns -1.\n", fd);
printf("perror() tells us why: ");
fflush(stdout);
i = lseek(fd, -1, SEEK_SET);
perror("l1");
}
Output:
(4.2) Compile and execute lseek_example2.c with input file testfile.txt, both available from eLEAP.
(Include the source code of this program, how to compile, and the output). Explain the output
(include in Lab report)
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdio.h>
int main()
{
int file=0;
if((file=open("testfile.txt",O_RDONLY)) < -1)
return 1;
char buffer[19];
if(read(file,buffer,19) != 19) return 1;
printf("%s\n",buffer);
return 0;
}
Output: