Embedded Linux Unit3 Notes
Embedded Linux Unit3 Notes
System programming involves writing code that interacts closely with the operating system. It
includes interacting with hardware, managing memory, handling processes, file I/O, etc.
Key concepts:
- System calls: Functions provided by the OS kernel (e.g., read, write, open, fork)
- Headers: unistd.h (for system calls), fcntl.h (file control), sys/types.h
Linux allows low-level file operations using system calls like open(), read(), write(), close().
Example:
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("file.txt", O_CREAT | O_WRONLY, 0644);
write(fd, "Linux File I/O\n", 15);
close(fd);
return 0;
}
Working with low-level I/O operations using system calls like read() and write().
Example:
char buf[100];
int n = read(fd, buf, 100); // Reads 100 bytes from fd
write(1, buf, n); // Writes to stdout
Example:
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid = fork();
if (pid == 0) {
execlp("ls", "ls", NULL);
} else {
wait(NULL);
printf("Child finished\n");
}
return 0;
}
Example:
#include <pthread.h>
int counter = 0;
pthread_mutex_t lock;
int main() {
pthread_t t1, t2;
pthread_mutex_init(&lock, NULL);
pthread_create(&t1, NULL, increment, NULL);
pthread_create(&t2, NULL, increment, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_mutex_destroy(&lock);
printf("Counter: %d\n", counter);
return 0;
}
Follow-up Q&A