Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

SP Programming Model Program

Uploaded by

sakeshanp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SP Programming Model Program

Uploaded by

sakeshanp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

Define a data structure of type 'book' containing the info 'book name', 'author', 'pages' &
'price'. Write a program to read & display these data using pointer.

#include <stdio.h>

#include <stdlib.h>

struct Book {

char name[100];

char author[100];

int pages;

float price;

};

int main() {

struct Book *ptr;

ptr = (struct Book *)malloc(sizeof(struct Book));

if (ptr == NULL) {

printf("Memory allocation failed. Exiting program.\n");

return 1;

printf("Enter book name: ");

scanf("%s", ptr->name);

printf("Enter author name: ");

scanf("%s", ptr->author);

printf("Enter number of pages: ");

scanf("%d", &ptr->pages);

printf("Enter price: ");

scanf("%f", &ptr->price);

printf("\nBook Information:\n");

printf("Name: %s\n", ptr->name);

printf("Author: %s\n", ptr->author);

printf("Pages: %d\n", ptr->pages);

printf("Price: $%.2f\n", ptr->price);

free(ptr);
return 0;

2. Write a simple function in 'C' that returns a structure. Demonstrate with an example.

#include <stdio.h>

struct Person {

char name[100];

int age;

};

struct Person createPerson() {

struct Person newPerson;

printf("Enter person's name: ");

scanf("%s", newPerson.name);

printf("Enter person's age: ");

scanf("%d", &newPerson.age);

return newPerson;

int main() {

struct Person myPerson = createPerson();

printf("\nPerson Information:\n");

printf("Name: %s\n", myPerson.name);

printf("Age: %d\n", myPerson.age);

return 0;

3. Write a program to multiply two - 8bit numbers using 8051 or any other microcontroller.

#include <stdio.h>

unsigned char multiply8bit(unsigned char a, unsigned char b) {

return a * b;

}
int main() {

unsigned char num1 = 0x0A;

unsigned char num2 = 0x05;

unsigned char result = multiply8bit(num1, num2);

printf("Result of %d * %d = %d\n", num1, num2, result);

return 0;

4. Write the steps taken after receiving an interrupt in 8051 architecture. Give an example

PROGRAM EXPLANATION

ORG 0x00 ;

START:

MOV TMOD, #01 ;

MOV TH0, #0xFF ;

MOV TL0, #0x00

SETB ET0 ;

SETB EA ;

SETB TR0 ;

MAIN_LOOP:

NOP ;

TIMER0_ISR:

RETI ;

5. Create two threads 'T1' & 'T2'. The thread T1 shall read the data from stdin continuously, the
other thread 'T2' will write the data by 'T1' to a file "output.txt". Develop a program in C to
demonstrate this.

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

struct SharedData {

FILE *file;

pthread_mutex_t mutex;
int stopFlag;

};

void *threadT1(void *arg) {

struct SharedData *sharedData = (struct SharedData *)arg;

char buffer[100];

while (!sharedData->stopFlag) {

printf("Thread T1: Enter data (or type 'exit' to stop): ");

fgets(buffer, sizeof(buffer), stdin);

pthread_mutex_lock(&sharedData->mutex);

fprintf(sharedData->file, "%s", buffer);

pthread_mutex_unlock(&sharedData->mutex);

if (strcmp(buffer, "exit\n") == 0) {

sharedData->stopFlag = 1;

return NULL;

void *threadT2(void *arg) {

struct SharedData *sharedData = (struct SharedData *)arg;

while (!sharedData->stopFlag) {

usleep(100000);

return NULL;

int main() {

FILE *outputFile = fopen("output.txt", "w");

if (outputFile == NULL) {

perror("Error opening file");

return 1;

struct SharedData sharedData;


sharedData.file = outputFile;

sharedData.stopFlag = 0;

pthread_mutex_init(&sharedData.mutex, NULL);

pthread_t tid1, tid2;

pthread_create(&tid1, NULL, threadT1, &sharedData);

pthread_create(&tid2, NULL, threadT2, &sharedData);

pthread_join(tid1, NULL);

pthread_join(tid2, NULL);

pthread_mutex_destroy(&sharedData.mutex);

fclose(outputFile);

return 0;

6. Write a program to copy the contents of a text file "a.txt" to another file "b.txt" & count the
occurrence of 'the' in the file "a.txt".

#include <stdio.h>

#include <stdlib.h>

int main() {

FILE *fileA, *fileB;

char ch;

int count = 0;

fileA = fopen("a.txt", "r");

fileB = fopen("b.txt", "w");

if (fileA == NULL || fileB == NULL) {

printf("Error opening files.\n");

exit(1);

while ((ch = fgetc(fileA)) != EOF) {

fputc(ch, fileB);

if (ch == 't') {

ch = fgetc(fileA);
if (ch == 'h') {

ch = fgetc(fileA);

if (ch == 'e') {

count++;

ungetc(ch, fileA);

} else {

ungetc(ch, fileA);

printf("Occurrences of 'the' in a.txt: %d\n", count);

fclose(fileA);

fclose(fileB);

return 0;

7. Write a signal handler in C which prints your name when you press "ctrl+c".

#include <stdio.h>

#include <signal.h>

void signalHandler(int signum) {

printf("\nSignal received. Hello, I'm Anand!\n");

int main() {

signal(SIGINT, signalHandler);

while (1) {

printf("Press Ctrl+C to trigger the signal...\n");

return 0;

}
8. Write a program to demonstrate two processes communicating with each other using shared
memory.

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <string.h>

struct SharedData {

int number;

char message[100];

};

int main() {

int shmid;

key_t key = ftok("shared_memory_example", 65);

shmid = shmget(key, sizeof(struct SharedData), IPC_CREAT | 0666);

if (shmid == -1) {

perror("shmget");

exit(1);

struct SharedData *sharedData = (struct SharedData *)shmat(shmid, NULL, 0);

if ((intptr_t)sharedData == -1) {

perror("shmat");

exit(1);

pid_t pid = fork();

if (pid == -1) {

perror("fork");

exit(1);

} else if (pid == 0) {
printf("Child Process:\n");

printf("Number: %d\n", sharedData->number);

printf("Message: %s\n", sharedData->message);

shmdt(sharedData);

} else {

printf("Parent Process:\n");

sharedData->number = 42;

strcpy(sharedData->message, "Hello from parent!");

wait(NULL);

shmdt(sharedData);

shmctl(shmid, IPC_RMID, NULL);

return 0;

9. Write a program to add a data byte located at offset 0x0500 in 0x2000 usegment to another
data byte at 0x0600 in the same segment & store the result at 0x700 in the same segment

#include <stdio.h>

int main() {

char userSegment[0x2000];

userSegment[0x0500] = 0x0A;

userSegment[0x0600] = 0x05;

userSegment[0x0700] = userSegment[0x0500] + userSegment[0x0600];

printf("Result: %d\n", userSegment[0x0700]);

return 0;

10. Write a program in Assembly for a 16-bit increment without affecting the contents of the
accumulator

section .data

value dw 1234h

section .text

global _start
_start:

mov ax, [value]

inc ax

; AX now contains the incremented value, and the original value is still in [value]

; Additional code or output

; Exit the program

mov eax, 1

xor ebx, ebx

int 0x80

11. Explain the memory layout of 8051 or any microcontroller

8051 Memory Organization - ROM and RAM Structure (technobyte.org)

12. Write a program using Linux timer to generate an alarm periodically

#include <stdio.h>

#include <signal.h>

#include <unistd.h>

void alarmHandler(int signum) {

printf("Alarm! Time to wake up!\n");

int main() {

signal(SIGALRM, alarmHandler);

alarm(2);

while (1) {

printf("Doing some work...\n");

sleep(1);

return 0;

You might also like