i am having difficulties implementing shared memory with this code
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
at the moment x is just a normal variable but if i want to fork it using a parent.c file how can i use a shared memory here so that both producer and consumer runs together
i am unable to use shared memory with this code also notable to find a good documentation online
Related
#include <stdio.h>
#define max 20
int stk[max];
int top=-1;
void push(int);
int pop();
void peep();
void display();
int isFull();
int isEmpty();
int main()
{
int ch,item;
do
{
printf("....Stack Operations....\n");
printf("Press 1 for Push\n");
printf("Press 2 for pop\n");
printf("Press 3 for peep\n");
printf("Press 4 for display\n");
printf("Enter your choice \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the item :- \n");
scanf("%d",&item);
push(item);
break;
case 2:
int k=pop();
printf("The poped element is %d\n",k);
break;
case 3:
peep();
break;
case 4:
display();
break;
default:
printf("Invalid Choice\n");
}
}while(ch>=1 && ch<=5);
void push(int item)
{
if(isFull())
printf("Stack Overflow\n");
else
{
top=top+1;
stk[top]=item;
}
}
int pop()
{
int s;
if(isEmpty())
printf("Stack Underflow\n");
else
{
s=stk[top];
top=top-1;
}
return s;
}
void peep()
{
if(isEmpty())
printf("Stack Underflow\n");
else
{
printf("Topmost Element of the stack is %d",stk[top]);
}
}
void display()
{
if(isEmpty())
printf("Stack Underflow\n");
else
{
for(int i=top;i>=0;i--)
printf("%d ",stk[i]);
}
}
int isFull()
{
if(top==max-1)
return 1;
else
return 0;
}
int isEmpty()
{
if(top==-1)
return 1;
else
return 0;
}
}
I wanted the program to run for all stack operations but I am getting this type of specific error:-
/usr/bin/ld: /tmp/ccHJ1ZkB.o: in function `main':
main.c:(.text+0xf7): undefined reference to `push'
/usr/bin/ld: main.c:(.text+0x103): undefined reference to `pop'
/usr/bin/ld: main.c:(.text+0x12b): undefined reference to `peep'
/usr/bin/ld: main.c:(.text+0x137): undefined reference to `display'
collect2: error: ld returned 1 exit status
It's not standard C to define functions within another function (main()) aka "nested function".
pop() returns an undefined value if the stack is empty. I changed it to return -1 but really you want to add the ability to tell caller of errors like this.
You need a block {} when introducing variables in a switch statement.
(not fixed) Avoid global variables and instead pass values to the functions that need them (functional style). Usually, you then group related data (stk and top) into a struct.
(not fixed) By convention constants, like max, should be upper case.
#include <stdio.h>
#define max 20
int stk[max];
int top=-1;
int isFull() {
return top==max-1;
}
int isEmpty() {
return top==-1;
}
void push(int item) {
if(isFull()) {
printf("Stack Overflow\n");
return;
}
stk[top++]=item;
}
int pop() {
if(isEmpty()) {
printf("Stack Underflow\n");
return -1;
}
return stk[top--];
}
void peep() {
if(isEmpty()) {
printf("Stack Underflow\n");
return;
}
printf("Topmost Element of the stack is %d",stk[top]);
}
void display() {
if(isEmpty()) {
printf("Stack Underflow\n");
return;
}
for(int i=top; i>=0; i--)
printf("%d ", stk[i]);
}
int main() {
int ch;
do {
printf(
"....Stack Operations....\n"
"Press 1 for Push\n"
"Press 2 for pop\n"
"Press 3 for peep\n"
"Press 4 for display\n"
"Enter your choice \n"
);
scanf("%d",&ch);
switch(ch) {
case 1: {
printf("Enter the item :- \n");
int item;
scanf("%d", &item);
push(item);
break;
}
case 2:
printf("The popped element is %d\n", pop());
break;
case 3:
peep();
break;
case 4:
display();
break;
default:
printf("Invalid Choice\n");
}
} while(ch>=1 && ch<=5);
}
I'm doing self-study on C Programming, and I have been recommended the following C Program by my colleagues to study further, where you can enter the Name and Age and it displays and uses Insert, Delete, Display, and Exit menu options.
I'm trying to convert it to my current study stream logic scenario where I need to enter the Name, Exercise Mark 1 (up to 3), and then it computes the Average and gets displayed while employing the Insert, Delete, Display, Update (updating the scores only, not the names), Delete and Exit.
Any guidance please on how to learn this code and understand the logic, and apply it to the 2nd scenario will be much appreciated.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 50
//using parallel arrays as fields in the list
typedef struct list{
char name[MAX][31];
int age[MAX];
int last;
}LIST;
LIST L;//L structure is global
void save();
void retrieve();
void makenull();
void insert(char n[31],int a);
void del(char n[31]);
void display();
int locate(char n[31]);
int isfull();
int isempty();
int menu();
int main(){
char nm[31];
int ag;
makenull();
retrieve();
while(1){
switch(menu()){
case 1: system("cls");printf("Insert Mode\n");
printf("Input Name: ");scanf("%s",nm);
printf("Input Age: ");scanf("%d",&ag);insert(nm,ag);break;
case 2: system("cls");printf("Delete Mode\n");
printf("Input Name: ");scanf("%s",nm);del(nm);break;
case 3: display();break;
case 4: save();exit(0);
default: printf("\n1-4 lang!\n");system("pause");
}
}
return 0;
}
void makenull(){
L.last = -1;
}
void insert(char n[31],int a){
if (isfull()){
printf("List is full.\n");
system("pause");
}
else {
L.last++;
strcpy(L.name[L.last],n);
L.age[L.last]=a;
}
}
void del(char n[31]){
int p;
if (isempty()){
printf("List is empty.\n");
system("pause");
}
else {
p=locate(n);
if (p==-1){
printf("Not found.\n");
system("pause");
}
else{
for(int i = p;i<L.last;i++){
strcpy(L.name[i],L.name[i+1]);
L.age[i]=L.age[i+1];
}
L.last--;
printf("Successful delete operation.\n");
system("pause");
}
}
}
void display(){
int i;
system("cls");
printf(" Name Age \n");
for(i=0;i<=L.last;i++)
printf("%d.) %s %d\n",i+1,L.name[i],L.age[i]);
system("pause");
}
int locate(char n[31]){
int i;
for (i=0;i<=L.last;i++)
if(strcmp(L.name[i],n)==0)
return i;
return -1;
}
int isfull(){
if (L.last==MAX-1)
return 1;
else
return 0;
}
int isempty(){
return(L.last==-1);
}
int menu(){
int op;
system("cls");
printf("MENU\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("\nSelect(1-4): ");
scanf("%d",&op);
return(op);
}
void save(){
FILE *fp;
int i;
fp=fopen("Practice4.dbf","w+");
if (fp==NULL){
printf("File Error.\n");
system("pause");
}
else{
for (i=0;i<=L.last;i++)
fprintf(fp,"%s %d\n",L.name[i],L.age[i]);
}
fclose(fp);
}
void retrieve(){
FILE *fp;
char n[31];
int i,a;
fp=fopen("Practice4.dbf","r+");
if (fp==NULL){
printf("File Error.\n");
system("pause");
}
else {
while(!feof(fp)){
fscanf(fp,"%s %d\n",n,&a);
insert(n,a);
}
}
fclose(fp);
}
Your code isn't properly formatted and there are no comments. I can't give you a direct answer with some code in it, but summing up all my comments (and of course I deleted them), this is what I've to say:
Consider this scenario-
if your .dbf has more than MAX 50 elements, then your while (!feof(fp)) inside retrieve() will keep calling insert() and insert() will keep executing its if () { } block.
You should put something like while (!feof(fp) && L.last < MAX) to prevent that situation and you'll need to further modify your code in insert(). Another thing is, this code doesn't have any update() function and scores variable. You'll need to add scores in your struct as well as there must be scores fields in your .dbf.
Now, for a moment let's say everything else is good to go in your code, then you should follow these following steps:
Declare variables
char nameInput[31];
float ex_marks[3], sum = 0, avr = 0;
in main().
Add another case 5 in your switch () block inside main() and translate and convert the following pseudocode into C code:
Read name in nameInput
locate()
if found then
3.a for i = 0 to 2
Read marks in ex_marks[i]
sum = sum + ex_marks[i]
3.b Calculate avr = sum / 3
3.c Display name and avr
else
Display name is not in the list.
exit
Also read about why is while(!feof()) always wrong?
I know I for sure misunderstand how to use pointers again. So here is my code. Would be nice if you all can help me. The program is simple. You write values in a structure array and print them out. Even so it would be nice if someone could explain to me when to use double pointers and how to use them probably.
#include <stdio.h>
#include <stdlib.h>
#define MAXA 3
typedef enum{
FOOD,
ART,
OTHERS
}TKindOfArticle;
typedef struct{
int number;
char description[31+1];
int sellingGrossPrice;
int vat;
int minimumStockLevel;
TKindOfArticle kindOf;
}TArticle;
void readOneArticle(TArticle* arti);
int readMaxArticle(TArticle* arti[]);
void printfOneArticle(TArticle arti);
void printfMaxArticle(TArticle *arti[],int read);
int main()
{
TArticle arti[MAXA];
int howMany;
howMany = readMaxArticle(&arti);
printfMaxArticle(&arti,howMany);
return 0;
}
void readOneArticle(TArticle* arti){
printf("Number: ");
scanf("%d", &(arti->number));
printf("Descrip: ");
scanf("%s", &(arti->description));
printf("SellGrossPrice: ");
scanf("%d",&(arti->sellingGrossPrice));
printf("MinimumStock: ");
scanf("%d",&(arti->minimumStockLevel));
printf("Kind of article (0: Food, 1: Art, 2: Others): ");
scanf("%d",&(arti->kindOf));
if(arti->kindOf == FOOD){
arti->vat= arti->sellingGrossPrice*1.1;
} else if(arti->kindOf == ART){
arti->vat= arti->sellingGrossPrice*1.13;
}else if(arti->kindOf == OTHERS){
arti->vat= arti->sellingGrossPrice*1.2;
}
}
int readMaxArticle(TArticle* arti[]){
int read;
int i=0;
printf("Max Elements (max. 3): ");
scanf("%d",&read);
if(read>MAXA){
printf("Error");
} else{
for(i=0; i<read;i++){
readOneArticle(arti[i]);
printf("\n");
printf("Number: %d\nDescrip.: %s\nSell Gross: %d\nVat: %d\nMin. Stock: %d\n",
(*arti[i]).number,(*arti[i]).description,
(*arti[i]).sellingGrossPrice,(*arti[i]).vat,(*arti[i]).minimumStockLevel);
}
}
return read;
}
void printfOneArticle(TArticle arti){
printf("Number: %d\nDescrip.: %s\nSell Gross: %d\nVat: %d\nMin. Stock: %d\n",
arti.number,arti.description,
arti.sellingGrossPrice,arti.vat,arti.minimumStockLevel);
switch(arti.kindOf){
case 0: printf("Kind: Food\n");
break;
case 1: printf("Kind: Art\n");
break;
case 2: printf("Kind: Others\n");
break;
}
}
void printfMaxArticle(TArticle *arti[],int read){
if(read>MAXA){
} else{
for(int i=0; i<read;i++){
printfOneArticle(*arti[i]);
printf("\n");
}
}
}
You are creating an array of structures: TArticle arti[MAXA];
But when you call readMaxArticle(&arti) function you pass pointer to array of TArticle. TArticle *arti[] it reads like this - arti is array of pointers to TArticle. But you want to pass pointer to array of TArticle. It won't compile anyway.
error: cannot convert 'TArticle (*)[3]' to 'TArticle**'
You might want to look at how to read C declaration and if you want to practice visit this site.
I want to implement circular FIFO buffer in C. While I was searching for code samples on Google I found this link. I don't understand is this mean that the concept of circular FIFO buffer is patented and everyone who want to use it must have a license?
Best wishes
IANAL
No, FIFOs in software are not patented (there were no software patents when the first FIFO algorithm was invented - I'm not sure of the exact dates but software patents are a phenomenon of the 1990's and I used FIFOs on the C64 in 1983).
What is patented by the patent is a hardware chip that contains a FIFO and which has certain characteristics, especially that "multiple transfers are performed during one bus cycle".
So this is not your general 16550 UART (which can do only a single transfer per clock cycle).
Generally, a lot of code that you writer every day is patented. Usually, this is not an issue because your company isn't on the radar of the owner of the patent. But the day they decide that they don't like you anymore, you're in big trouble unless you can spend a couple of hundreds of millions of dollars for lawyer fees to defend yourself in court or you have a huge stack of silly patents yourself which you can use to fight back.
I collected some some articles that you might want to read:
Martin Fowler Chimes Into Chorus Against Software Patents
Patent Trolls vs Common Sense 1:0 Again
typedef struct red
{
int niz[MAX];
int f, r;
} RED;
int insert(RED buf, int info)
{
if (isFull(buf))
buf->f = (buf->f + 1) % MAX;
buf->r = (buf->r + 1) % MAX;
buf->niz[buf->r] = info;
return 1; }
// circular_queue.cpp : main project file.
#include "stdafx.h"
using namespace System;
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define max 3
int q[10];
int front=0;
int rear=-1;
int main()
{
int ch;
void insert();
void delet();
void display();
// clrscr();
printf("\n Circular Queue operations\n");
printf("1.insert \n 2.delete \n 3.display \n 4.exit \n");
while(1)
{
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(); break;
case 2: delet(); break;
case 3: display(); break;
case 4:exit(0);
default:printf("Invalid option\n");
}
}
return 0;
}
void insert()
{
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
scanf("%d",&x);
if(rear==max-1&&front>0)
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
getch();
exit(0);
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else
if(front==max-1)
{
a=q[front];
front=0;
}
else a=q[front++];
printf("Deleted element is:%d\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
getch();
exit(0);
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%d",q[i]);
for(j=front;j<=max-1;j++)
printf("\t%d",q[j]);
printf("\n rear is at %d\n",q[rear]);
printf("\n front is at %d\n",q[front]);
}
else
{
for(i=front;i<=rear;i++)
{
printf("\t%d",q[i]);
}
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
printf("\n");
}
//getch();
My function code for peek is not working? why is that? can anyone help me with my peek function?
#include<stdio.h>
#include<stdlib.h>
#define maxsize 10
int stack[maxsize];
int stacktop=0;
void instructions();
int process();
int push(int value);
int pop();
void display();
void peek();
int main()
{
process();
getch();
}
int process()
{
int val;
int choice;
do
{
instructions();
printf("Enter Your Choice: ");
scanf("%d",&choice);
switch( choice )
{
case 1:
printf("\nElement to be Pushed : ");
scanf("%d",&val);
push(val);
break;
case 2:
val=pop();
if(val!=-1)
{
printf("Popped Element : %d\n",val);
}
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
break;
}
}while(choice !=5);
}
void instructions()
{
printf("Enter Your choice for the following process\n");
printf("\n[1]Push a Node on top of the list");
printf("\n[2]Pop a node off the list");
printf("\n[3]Peek The Top Node");
printf("\n[4]Display The Whole list");
printf("\n[5]Exit The Program\n");
}
int push(int val)
{
if(stacktop<maxsize)
{
stack[stacktop++]=val;
}
else
{
printf("Stack is full");
}
}
int pop()
{
int a;
if(stacktop>0)
{
a=stack[--stacktop];
return a;
}
}
void display()
{
int i;
i = 0;
if(stacktop>0)
{
printf("Elements are:");
while(i<stacktop)
{
printf("\n%d--\n",stack[i++]);
}
}
}
void peek()
{
printf("%d",stacktop);
}
Is it supposed to be:
printf("%d\n", stack[stacktop - 1]);
Print the contents, rather than the size of the stack?
Obviously you'd also need to bounds check to make sure you're not printing outside of the range of your stack (when it's empty)
I know this isn't Code Review, but I thought I would give you a few bits of advice.
When you call scanf, always check the result. For example, if the user enters something other than a decimal number, your code will end up putting an indeterminate value into the choice or val variables. The scanf function returns the number of items that were successfully read. If you asked for one item, and scanf returns 1, then you can rely on the value of that object:
int choice;
if (scanf("%d", &choice) != 1)
// handle error, can't rely on value of "choice"
else
// continue onwards, can rely on value of "choice"
Usually, the \n escapes go at the end of the string literal, not at the beginning. It is more common to do it this way, but it doesn't mean it should always go at the end.
printf("Enter Your choice for the following process\n\n");
printf("[1]Push a Node on top of the list\n");
printf("[2]Pop a node off the list\n");
printf("[3]Peek The Top Node\n");
For outputting simple strings, consider just using the puts function, which automatically appends the new-line character for you:
puts("Enter Your choice for the following process");
puts("");
puts("[1]Push a Node on top of the list");
puts("[2]Pop a node off the list");
puts("[3]Peek The Top Node");
Your display method is a perfect example of when to use a for loop instead of a while loop. Generally speaking, use a for loop when you know exactly how many items you have and you want to iterate over each of them:
void display()
{
int i;
puts("Elements are:");
for (i = 0; i < stacktop; i++)
printf("\n%d--\n", stack[i]);
}
To reverse the order of the stack, simply start at the top and go backwards:
void display()
{
int i;
puts("Elements are:");
for (i = stacktop - 1; i >= 0; i--)
printf("\n%d--\n", stack[i]);
}